2016-07-29 73 views
2

我的应用程序允许人们在特定位置创建地理围栏。它工作正常,但我似乎无法删除geofences。 这里是我的代码去除所有的地理栅栏。地理栅栏不能删除?

public void removeAllGeofences(GeofenceControllerListener listener) { 
    namedGeofencesToRemove = new ArrayList<>(); 
    for (NamedGeofence namedGeofence : namedGeofences) { 
     namedGeofencesToRemove.add(namedGeofence); 
    } 
    this.listener = listener; 

    connectWithCallbacks(connectionRemoveListener); 
} 

这似乎并不奏效,除非我重新启动手机,否则地理栅栏将保持不变。 如何帮助或解释geofences的工作方式真的有帮助!

回答

0

当不再需要或不需要停止地理围栏监控时,可以帮助节省设备上的电池电量和CPU周期。您可以在用于addremove地理栅栏的主要活动中停止geofence monitoring;删除地理围栏会立即停止。该API提供了通过请求ID删除地理围栏的方法,或通过删除与给定PendingIntent相关的地理围栏来删除地理围栏。

下面的代码片段通过的PendingIntent删除地理围栏,停止所有进一步的通知时,设备进入或退出之前添加地理围栏:

LocationServices.GeofencingApi.removeGeofences(
mGoogleApiClient, 
// This is the same pending intent that was used in addGeofences(). 
getGeofencePendingIntent() 
).setResultCallback(this); // Result processed in onResult(). 
} 

您可以与其他位置感知功能,如周期性位置更新相结合地理围栏。有关更多信息,请参阅本课程中的其他课程。

temporary disable要存储地理围栏参数某处在你的应用(例如,在源码或共用首选项),然后从GeofencingApi将其取出并恢复(重新启用)经由addGeofences需要时。