【发布时间】:2015-06-25 21:30:29
【问题描述】:
我正在尝试从由 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 实例(request 和 remove 方法的必需参数)的正确方法是什么?或者,如果这是错误的问题,那么完成我想要做的事情的正确方法是什么?
编辑(2015 年 4 月 19 日): 以下是相关的代码部分...
来自我的类,它扩展了监听 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 传递的意图的 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);
}
【问题讨论】:
-
你能显示你的代码吗?我刚刚使用 Google Play Services Fused Location API 完成了一个项目。一个关键是,在调用
requestLocationUpdates()和removeLocationUpdates()之前,我检查了GoogleApiClient不为空,并且已连接。 -
我会添加代码的相关部分以便您看到它。但是,在这个阶段,检查客户端是否为空是没有意义的。在当前的设置中,它将始终为 null,因此我不需要检查它。
标签: android location intentservice google-api-client