2016-07-29 42 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需要時。