2017-02-17 52 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())包裝您的聲明。然後,如果它返回false,我會在代碼中查找我已經停止的代碼,即mGoogleApiClient.disconnect()

注意,當Activity破壞,因此可能是GoogleApiClient已經被系統斷開onDestroy()被調用。 (雖然我也注意到你說你已經在onStop()中試過了)