0

在我的项目,我使用“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(); 
} 

我已经使用IntentServiceGeocoder得到的地址!

+0

您可以尝试采用Android服务和定时器与服务 –

回答

0

入住onRestart是GPS的启用或不受该条件

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

我已经做了,我要的是东西,不断尝试,直到连接恢复! –

+0

使用处理程序,然后它会保持在给定的时间后检查GPS是否可用 –

0

把处理程序在连接中止或试图检查和连接失败方法如果不为null重新连接。

+0

我不能做手工!它必须是一个定时器的一些还挺所以每5秒后重试! –

+0

您可以使用处理程序类方法scheduleatFixedRate来设置它,您必须在其中指定该任务应该执行的时间间隔 –

相关问题