Android - GoogleApiClient 实例在 IntentService 中的 .connect() 和 onConnected() 之间丢失

标签 android location intentservice google-api-client

我正在尝试管理由 GPS 启用状态更改触发的 IntentService 的启动和停止位置更新(以及其他连接的操作)。问题的出现是因为 GoogleApiClient 上的 .connect() 方法异步运行,这导致服务终止。当收到 onConnected() 的回调时,静态全局变量 mGoogleApiClient 已被删除。对于removeLocationUpdates()我得到:

java.lang.NullPointerException: GoogleApiClient must not be null

对于 requestLocationUpdates() 我得到:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

在 IntentService 死亡和重生之间保留 GoogleApiClient 实例(这是请求和删除方法的必需参数)的正确方法是什么?或者,如果这是错误的问题,那么完成我想做的事情的正确方法是什么?

编辑(2015 年 4 月 19 日): 这是代码的相关部分...

来 self 的类​​,它扩展了监听 GPS 事件的 WakefulBroadcastReceiver:

public void onReceive(Context context, Intent intent) {
    // If user enables or disables GPS
    if(intent.getAction().equals(LocationManager.MODE_CHANGED_ACTION) || intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
        Intent mIntent = new Intent(context, ActivationIntentService.class);
        mIntent.putExtra("enabled", isGPSEnabled(context));
        startWakefulService(context, mIntent);
    }
}

来自接收从 startWakefulService 传递的 Intent 的 ActivationIntentService 类:

@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        this.intent = intent;
        if(intent.hasExtra("enabled")) {
            if(intent.getBooleanExtra("enabled", false)) {
                buildGoogleApiClient();
            }
            else {
                stopUpdates();
            }
        }
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .addApi(ActivityRecognition.API)
            .build();
    mGoogleApiClient.connect();
}

protected LocationRequest getLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(5000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}

@Override
public void onConnected(Bundle bundle) {
    startLocationUpdates();
    startActivityRecognitionUpdates();
    ActivationReceiver.completeWakefulIntent(intent);
}

public void stopUpdates() {
    PendingIntent locationPendingIntent = PendingIntent.getService(this, 1, new Intent(this, LocationIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,locationPendingIntent);
    PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 2, new Intent(this, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mGoogleApiClient,activityRecognitionPendingIntent);
}

public void startLocationUpdates() {
    PendingIntent locationPendingIntent = PendingIntent.getService(this, 1, new Intent(this, LocationIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, getLocationRequest(), locationPendingIntent);
}

public void startActivityRecognitionUpdates() {
    PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 2, new Intent(this, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 5000, activityRecognitionPendingIntent);
}

最佳答案

我的第一个怀疑是正确的。我试图解决错误的问题。

通过跟踪几乎每个方法调用进行详细调试后,我意识到系统由于某种原因两次广播 GPS 模式更改,因此在第一个 onConnected 回调时已实例化第二个 GoogleApiClient(覆盖第一个实例)从第一次 .connect() 调用中被触发。我的方法使用第二个(未连接的)客户端变量而不是第一个客户端变量来启动或停止更新,这导致引发异常。由于每次用户启用或禁用 GPS 时系统都会触发两个广播(对我来说似乎有点愚蠢),因此我在 BroadcastReceiver 中设置了一个标志,以便可以忽略第二个广播。问题已解决。

关于Android - GoogleApiClient 实例在 IntentService 中的 .connect() 和 onConnected() 之间丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726161/

相关文章:

Android - 确定用户速度的可能方法

android - 创建长期运行的服务

java - 在 android canvas 上绘制一堆旋转的矩形

java - Android 布局快速问题

android - 静态谷歌地图 : Is it possible to add Text to the Marker

java - Android 使用单个光标获取多个内容提供程序

iphone - longpress手势识别器不适用于 map View 上的用户位置(MKuserlocation)-iOS

javascript - window.location 与位置

android - 将 Activity 实例保存在 WeakReference 中是一种好习惯吗

Android:如何在 IntentService 中获取调用 Activity 的上下文?