Do Android Geofences remain active until removed/expired or only until my PendingIntent is launched

孤街浪徒 提交于 2019-12-05 04:02:21

The good proposition is the first one. If you create a geofence with the flag NEVER_EXPIRE as expiration time, you won't have to re-register it when it is triggered (by going in or out). I'm 100% certain of this, I'm right now just finished coding and testing a POC about geofence.

From the doc, the only way for a geofence to be deleted is either expiration time is reached or it is deleted by the device itself.

Expiration time

How long the geofence should remain active. Once the expiration time is reached, Location Services deletes the geofence. Most of the time, you should specify an expiration time, but you may want to keep permanent geofences for the user's home or place of work.

To stop geofence monitoring, you remove the geofences themselves

Please remember that NEVER_EXPIRE will cause the geofence to be registered even after a user uninstalls the app in case the app doesn't uninstall them. There is no way to remove these. Ever. So they will keep draining battery. Therefore, setting an expiration time is advisable and to set the geofence again in case they expire before you want them to.

Be advised that "all registered geofences will be removed" in THIS case (i.e. when user disables Location is his phone settings).

BE ADVISED: at the top of that documentation is warning "This class is deprecated. Use LocationServices."

While you will get pending intent (transition) irrespective of how many times you enter/exit the geofence, the catch is that a device reboot will remove all your geofences. So in case of device reboot you must re-register all your geofences again (which you would have saved via shared preferences) using a broadcast receiver and set intent filter action

android.intent.action.BOOT_COMPLETED

and permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

in AndroidManifest.xml.

Also please remember that the system restores geofences even if the Google Play Services is killed+restarted/upgraded but not if you clear its data. The same is also mentioned in the developer docs under the section "Re-register geofences only when required"

Also note, in case user toggles OR switches off the location/gps setting, all the geofences will be removed and an intent is generated by the provided pending intent. In this case, hasError() api will return true and getErrorCode() api will return GEOFENCE_NOT_AVAILABLE.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!