如何创建一个IntentService来检查GoogleApiClient是否不是空android



在我的项目中,我使用"GoogleApiClient"每5秒发出一次位置请求,问题是,如果我关闭GPS,然后重新打开应用程序,它会崩溃,因为mGoogleApiClient对象当前为空!我只是在对onRestart进行简单的检查

protected void onRestart() {
super.onRestart();
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
startLocationUpdate();
}
}

我想要的是创建一些东西,每次对象mGoogleApiClient为 null 时我都可以调用它,这样它就可以在后台尝试,直到可以继续!在startLocationUpdate();里面我正在使用这个:

mlocationRequest = new LocationRequest();
mlocationRequest.setInterval(5000);
mlocationRequest.setFastestInterval(2000);
mlocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
try{
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mlocationRequest,MainActivity.this);
}catch (SecurityException ex){
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}

它可以是这样的:

if(mGoogleApiClient != null){
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mlocationRequest,MainActivity.this);
}else {
callServiceToTryConnection();
}

我已经将IntentServiceGeocoder一起使用以获取地址!

签入 GPS 是否启用此条件的重新启动

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//Here is your condition if GPS is not connected
}

将处理程序放入连接挂起或连接失败的方法,该方法尝试检查并重新连接(如果不是 null(。

最新更新