My app uses GoogleApiClient
to get the current location. It works well for the first time but if I close my app and then open it again, it crashes (on Sony Xperia Z3, Google Pixel).
On Asus, Samsung, ... it doesn't crash
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
at com.android.server.wm.WindowAnimator.access$000(WindowAnimator.java:53)
at com.android.server.wm.WindowAnimator$1.doFrame(WindowAnimator.java:123)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:856)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:234)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)
I found the line of code that causes a crash is GoogleApiClient.connect()
, I also use try-catch
, update to the latest Google API 11.8.0
but can not find a possible solution. Here is my code
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.v(">>>", "onLocationChanged Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude());
//Toast.makeText(this, "onLocationChanged Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude(), Toast.LENGTH_SHORT).show();
saveLocation(location);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
try {
Location mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {...}
//Toast.makeText(this, "onConnected Longitude = " + Constants.location.getLongitude() + ", Latitude = " + Constants.location.getLatitude(), Toast.LENGTH_SHORT).show();
}
startLocationUpdates();
} catch (Exception ex) {
// This will catch the exception, handle as needed
}
}
//Toast.makeText(this, "onConnected", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d("Location service", "onConnectionFailed");
}
@Override
protected void onResume() {
super.onResume();
checkPlayServices();
// Resuming the periodic location updates
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
startLocationUpdates();
}
Log.v(">>>", "onResume");
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect(); // CRASH HERE
Log.v(">>>", "GoogleApiClient.connect()");
}
Log.v(">>>", "onStart");
}
@Override
protected void onStop() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
mGoogleApiClient = null;
Log.v(">>>", "GoogleApiClient.disconnect()");
}
super.onStop();
Log.v(">>>", "onStop");
}
@Override
protected void onPause() {
stopLocationUpdates();
super.onPause();
Log.v(">>>", "onPause");
}
In onLocationChanged(location), saveLocation(location) uses RealmIO to save user's last location.
If I remove saveLocation(location), everything works well.
I found that if I use (RealmIO) or (SharedPreferences with Gson) to save data, the problem occurs
The temporary solution is using SharedPreferences without Gson:
SharedPreferences prefs = context.getSharedPreferences(
LAST_LOCATION, Context.MODE_PRIVATE);
prefs.edit().putString(LAST_LOCATION_LONGITUDE, location.getLongitude() + "").apply();
prefs.edit().putString(LAST_LOCATION_LATITUDE, location.getLatitude() + "").apply();
I don't know why the crash just happens in some specific devices. Hope this help someone!