GoogleApiClient.connect() 导致崩溃

GoogleApiClient.connect() causes crash

我的应用程序使用 GoogleApiClient 获取当前位置。它第一次运行良好,但如果我关闭我的应用程序然后再次打开它,它就会崩溃(在 Sony Xperia Z3,Google Pixel 上)。

在华硕、三星......它不会崩溃

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[=11=]0(WindowAnimator.java:53)
                                                   at com.android.server.wm.WindowAnimator.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)

我发现导致崩溃的代码行是GoogleApiClient.connect(),我也用try-catch,更新到最新的Google API 11.8.0但找不到可能的解决方案。这是我的代码

@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");
}

DeadObjectException - 您正在调用的对象已经终止,因为它的托管进程不再 exists.That 服务已经停止 - 要么从 OS 终止,要么从您的停止申请。

覆盖您的 onDestroy() 方法并观察导致 DeadObjectException 的事件。如果您在没有通过此方法的情况下捕获 DeadObjectException,您的服务应该已被 OS.

杀死

在 onLocationChanged(location) 中,saveLocation(location) 使用 RealmIO 保存用户的最后位置。

如果我删除 saveLocation(location),一切正常。

我发现如果我使用(RealmIO)或(SharedPreferences with Gson)保存数据,就会出现问题

临时解决方案是使用不带 Gson 的 SharedPreferences:

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();

我不知道为什么崩溃只发生在某些特定的设备上。希望这对某人有所帮助!