【发布时间】:2017-07-04 07:27:48
【问题描述】:
在我的项目中,我使用“GoogleApiClient”每 5 秒发出一次位置请求,问题是,如果我关闭 GPS 然后重新打开应用程序,它会崩溃,因为 mGoogleApiClient 对象当前为空!我只是在onRestart做一个简单的检查
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 Service 和 Timer with in service
标签: java android google-api-client intentservice android-intentservice