[英]How to create a IntentService to check if GoogleApiClient is not null android
在我的项目中,我使用“ GoogleApiClient”每5秒发出一次位置请求,问题是,如果我关闭GPS,然后重新打开App,它会崩溃,因为mGoogleApiClient
对象当前为null! 我只是在onRestart
上做一个简单的检查
protected void onRestart() {
super.onRestart();
if(mGoogleApiClient != null && mGoogleApiClient.isConnected()){
startLocationUpdate();
}
}
我想要的是创建一个每次对象mGoogleApiClient
为null时都可以调用的东西,这样它就可以在后台尝试,直到可以继续! 在startLocationUpdate();
内部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
一起使用来获取地址!
在此情况下检查是否启用了GPS的onRestart
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
//Here is your condition if GPS is not connected
}
将处理程序置于连接挂起或连接失败的方法中,该方法尝试检查并重新连接(如果不为null)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.