Search code examples
androidandroid-geofence

Re-register Geofences only when required


According to the Android geofencing documentation one should re-register the geofences if the following cases occur:

  • The device is rebooted.

Solution: The app should store the geofences internally (SharedPreferences) when the geofence is added. Then listen for the device's boot complete action, and re-register the stored geofences.

  • The app is uninstalled and re-installed.

Solution: User should create the geofences again or store them remotely and then retrieve when the app opens for the first time.

  • The app's data is cleared.

Solution: User should create the geofences again or store them remotely and then retrieve when the app opens for the first time.


However, I don't know what to do in the following cases, please can you advice?

  • Google Play services data is cleared. How can I detect this case?

  • The app has received a GEOFENCE_NOT_AVAILABLE alert. This typically happens after NLP (Android's Network Location Provider) is disabled. Not sure where this 'alert' is received


Solution

  • I found no way of detecting the case of Google Play services data is cleared, so I just request for new geofences whenever the app is opened.

    As @Sagar mentioned, GEOFENCE_NOT_AVAILABLE is received in the transitions service. This is received when the Location is disabled. To workaround this, I save the geofences (using SharedPreferences) whenever I request them, and then I have a BroadcastReceiver that detects when the Location is enabled again. I reload the saved geofences here.

    Thanks.