在我的项目,我使用“GoogleApiClient”,使地方要求各5秒,问题是,如果我把GPS关闭,然后重新打开应用程序,压伤因为mGoogleApiClient
对象是当前空!我只是做了简单的检查上onRestart
如何创建一个IntentService检查GoogleApiClient不为空的Android
protected void onRestart() {
super.onRestart();
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
startLocationUpdate();
}
}
我要的是创造的东西,每次对象mGoogleApiClient
是空,因此它可以在后台尝试,直到被罚款去,我可以叫!里面的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();
}
我已经使用IntentService
与Geocoder
得到的地址!
您可以尝试采用Android服务和定时器与服务 –