1

在使用融合位置提供程序的 Android Wear 上启动、关闭和重新启动活动时,我遇到了崩溃。

场景是:启动连接到融合的位置提供者的活动,关闭活动(同时与融合的位置提供者断开连接)并重新启动应用程序会导致新启动的活动崩溃,并出现以下异常:

01-31 18:51:15.319    2281-2281/com.example.test E/ActivityThread﹕ Performing pause of activity that is not resumed: {com.example.test/com.example.test.WearActivity}
java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.example.test/com.example.test.WearActivity}
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3196)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3184)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3159)
        at android.app.ActivityThread.access$1000(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

当 Android Wear 设备连接到手机并且融合位置提供程序返回“融合”位置时,可能会遇到崩溃。在没有连接电话的情况下使用位置提供程序时,不会出现崩溃。

当从应用程序列表重新启动活动时,新创建的活动会经历创建 -> 开始 -> 恢复阶段,然后直接进入停止而不暂停。

这会导致手表锁定,因此您无法再使用语音命令。不过,您仍然可以在主屏幕上滚动浏览卡片。按下设备按钮打开和关闭屏幕会“解锁”设备,logcat 会打印出上述异常。

有时它需要两次或多次重启应用程序才能导致崩溃,但它是可重现的。在 nexus 6 上运行相同的应用程序不会导致任何问题。

以下代码重现了该问题:

public class WearActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {

private GoogleApiClient apiClient;
private LocationListener locationListener;

@Override
protected void onStart() {
    super.onStart();
    Log.v("Wear", "onStart");
    apiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    apiClient.connect();

}

@Override
protected void onStop() {
    super.onStop();
    Log.v("Wear", "onStop");
    if (locationListener != null) {
        FusedLocationApi.removeLocationUpdates(apiClient, locationListener);
        System.out.println("removing");
        locationListener = null;
    }
    if (apiClient.isConnected()) {
        apiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    System.out.println("connection established");
    if (locationListener == null) {
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                System.out.println(location);
            }
        };
        FusedLocationApi.requestLocationUpdates(apiClient, createLocationRequest(), locationListener);
    }
}

private LocationRequest createLocationRequest() {
    return new LocationRequest()
            .setInterval(2000)
            .setFastestInterval(2000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnectionSuspended(int i) {
    System.out.println("connection suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    System.out.println("connection failed");
}
}

在运行 Android 5.0.1(构建 LWX48P)的 Sony Smartwatch 3 上看到。

4

1 回答 1

0

不确定这是否是问题,但无需每次都在 onStart 中创建新的 Google Api 客户端。相反,它应该在 onCreate 中,然后在 onStart 中只有 apiClient.connect();

于 2015-02-10T21:25:57.230 回答