删除活动结束时的所有活动地理围栏

时间:2017-02-17 13:47:19

标签: android google-maps android-geofence

我正在尝试在活动时删除所有活动的地理围栏。

以下是我正在使用的代码:

tag

我也尝试过:

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();
        }

如果我在onStop()方法中这样做,也会发生同样的事情。

最终结果是,保留了之前运行期间添加的地理围栏。我不希望这样。出了什么问题?

1 个答案:

答案 0 :(得分:0)

从错误消息中可以清楚地看出您的GoogleApiClient未连接。

尝试将您的陈述包装在if (mGoogleApiClient.isConnected())中。然后,如果它返回false,我会查看我的代码,因为我已将其停止,即mGoogleApiClient.disconnect()

请注意,onDestroy() 被销毁时会调用Activity,因此GoogleApiClient可能已被系统断开连接。 (虽然我也注意到你说你在onStop()

尝试过