2017-02-17 1 views
0

私の活動時にすべてのアクティブジオフェンスを削除しようとしています。ここで活動中のすべてのアクティブジオフェンスを削除します。

は、私が使用していますコードです:

private PendingIntent getGeofencePendingIntent() { 
     Intent intent = new Intent(this, GeofenceService.class); 
     // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling addgeoFences() 
     return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    } 

@Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(receiver); 
     //Setup permissions for location. 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(
        this, 
        new String[]{ 
          Manifest.permission.ACCESS_COARSE_LOCATION, 
          Manifest.permission.ACCESS_FINE_LOCATION 
        }, 
        LOCATION_PERMISSION_CODE 
      ); 
     } else { 
      if (lm != null) 
       lm.removeUpdates(this); 
     } 
     if (allRequestIds.size() != 0) { 
      LocationServices.GeofencingApi.removeGeofences(
        mGoogleApiClient, 
        allRequestIds 
      ); 
      allRequestIds.clear(); 
      filterMap.clear(); 
      registeredFences.clear(); 
      mGoogleApiClient.disconnect(); 
     } 
    } 

私も試してみました:

if (allRequestIds.size() != 0) { 
      LocationServices.GeofencingApi.removeGeofences(
        mGoogleApiClient, 
        getGeofencePendingIntent() 
      ); 
      allRequestIds.clear(); 
      filterMap.clear(); 
      registeredFences.clear(); 
      mGoogleApiClient.disconnect(); 
     } 

私はこのエラーを取得しています:

Unable to destroy activity, GoogleApiClient is not connected yet. 

私ならば、同じことが起こっていますonStop()メソッドで行います。

最終的には、前回実行時に追加されたジオフェンスが保持されます。私はそれを望んでいない。何がうまくいかないのですか?

答えて

0

エラーメッセージから、GoogleApiClientが接続されていないようです。

文章をif (mGoogleApiClient.isConnected())にラップしてみてください。それでもしそれが偽を返すなら、私はそれを停止した場所、すなわちmGoogleApiClient.disconnect()のために私のコードの周りを見ているでしょう。それはGoogleApiClientすでにシステムによって切断されたこととすることができるようActivityがはが破壊されるときonDestroy()が呼び出されること

注。 (でも、私はあなたがonStop()でそれを試してきたと言いますが)

関連する問題