有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建IntentService来检查GoogleAppClient是否为空

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

我已经用IntentServiceGeocoder来获取地址了


共 (2) 个答案

  1. # 1 楼答案

    在重新启动时签入,即GPS是否由该条件启用

    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
       //Here is your condition if GPS is not connected
    }
    
  2. # 2 楼答案

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