Android Wear上的融合位置提供程序会在重新启动活动时导致崩溃

时间:2015-02-07 10:12:27

标签: android wear-os fusedlocationproviderapi

在使用融合位置提供程序的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(Build LWX48P)的Sony Smartwatch 3上看到。

1 个答案:

答案 0 :(得分:0)

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