8

I'm about to implement a feature with geofences and before I get any serious work done, I need to know whether geofences remain active after the first transition.

The core of the feature is:

every time I'm within x meters of point P (call this Area A), I want Action B to occur.

What I need to know is

  • Do I just have to add a geofence with Geofence.NEVER_EXPIRE and rest assured that I will get a PendingIntent every time I enter the specified area regardless of elapsed time, reboots, etc

OR

  • Do I have to re-register this geofence once I exit Area A in order to get notified the next time I enter Area A?

I'm hoping that the former is the case

Marian Paździoch
  • 7,671
  • 9
  • 49
  • 88
copolii
  • 13,331
  • 9
  • 46
  • 76
  • Please see [my answer to a similar question](https://stackoverflow.com/a/50869301/766755) as I believe it should cover all the cases where you should re-register geofences based on the documentation. – Michael Krause Jul 26 '18 at 21:17

4 Answers4

10

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

Community
  • 1
  • 1
Marcel Falliere
  • 1,834
  • 21
  • 37
4

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.

hajons
  • 135
  • 3
  • 3
    A very valid point, but I wonder why it's implemented this way. If app X is uninstalled, we know for certain that nothing will be available to service the PendingIntent. I wonder if it's an bug ... it certainly seems so ... once you uninstall my app from your device, I have zero business remaining on your phone. – copolii Aug 01 '13 at 19:49
  • 10
    @hajons Is that in the documentation somewhere? How do you know that these fences are retained even after uninstall? – b-ryce Oct 21 '13 at 20:56
  • 2
    This doesn't seem to be true, as the [documentation](https://developer.android.com/training/location/geofencing.html) says that you have to re-register geofences when the app is uninstalled and re-installed. If they were retained across uninstalls that wouldn't be needed. – Mateus Gondim Nov 25 '16 at 14:27
  • 1
    I know the documentation said one thing, but that doesn't mean that is how it works. My answer was true at the time of writing, and verified by testing. Since then I assume it has been fixed. – hajons Nov 30 '16 at 09:42
1

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."

Marian Paździoch
  • 7,671
  • 9
  • 49
  • 88
1

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.

Uncaught Exception
  • 1,949
  • 14
  • 23