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()方法中執行。
最終結果是,在前面的運行過程中添加的地理柵欄被保留。我不想那樣。出了什麼問題?