How to increase consistency of Android geofence enter/exit notifications?

10,790

A couple of suggestions:

  1. Make sure you're using a BroadcastReceiver and not a Service to receive the transitions, otherwise you will not/might not get it if your app gets killed/turned off. As discussed here: Android Geofence eventually stop getting transition intents

  2. Make sure you recreate your geofences after the device is rebooted, preferably using a boot-broadcast receiver. As discussed here: Do Geofences remain active in android after a device reboot

  3. One other common misconception, and which stumped me since it's different than IOS is that you'll always get a trigger immediately for a newly created geofence, if the device discovers that you're inside the geofence when creating it. I have solved this myself using a "grace period" for newly created geofences, which i explained in this thread: addProximityAlert doesn't work as expected

Finally one important thing: Having your LocationClient connected in your app or not should not matter at all if you follow the points above. My process for adding or removing Geofences in my code is basically:

  1. Create and connect locationclient.

  2. In connect-callback, do adding/removing of geofence(s)

  3. In geofence-result callback, disconnect locationclient.

Every time this happens, my location client is only connected a few seconds in total. The operating system will still produce geofence alerts and call my BroadcastReceiver whenever they happen.

If you do these things I bet your experience will improve.

Hope this helps!

Share:
10,790

Related videos on Youtube

oviroa
Author by

oviroa

Updated on June 04, 2022

Comments

  • oviroa
    oviroa almost 2 years

    I am using the built in geofence APIs (play services) and have been having mixed results. It looks like after setting a geofence, the notifications for entering/exiting are very inconsistent even when GPS is on, with an up-to-date location (LocationClient connected, running in the background.) I started monitoring (polling) location changes and distances in a debug text field, and saw that, even when based on the location registered by the device and the location of the geofence I am technically inside/outside of the geofence, notifications are sometimes triggered and sometimes not. Any way to make this more predictable? I am almost tempted to abandon this api and implement my own (battery draining) geofences based on polling of device location.

  • oviroa
    oviroa over 10 years
    I will try #1. #2 and #3 are not applicable in my project, although it's overall useful information. Also, good to know that the LocationClient is not a relevant piece in this, since I put some effort into keeping it hot and have regular location updates running. Eliminating these will definitely make the application lighter and more battery/resource efficient.
  • Mathias
    Mathias over 10 years
    Great stuff, glad i could help out!
  • oviroa
    oviroa over 10 years
    Unfortunately, after making the suggested updates, nothing has improved. Also, this statement "Having your LocationClient connected in your app or not should not matter at all if you follow the points above." does not seem to be accurate as in some cases, I get no notifications when the client is disconnected, but as soon as I connect, they start coming in. All this is very frustrating since important features of my project depend on something that should work on paper, but in reality fails miserably.
  • Mathias
    Mathias over 10 years
    Sorry you're still having problems. I'm pretty sure though, that you don't need the LocationClient connected to get callbacks. The locationclient is a "channel" to perform location releated operations. I see it as making a phonecall. My broadcastintent gets callback after our app is destroyed, and after a phone reboot without having to actually start our app, so i'm pretty sure it works. If you look at the APIDocs for LocationClient, this is basically what it says there, too. Hope you work it out mate!
  • Anuj
    Anuj over 9 years
    Nice work @Mathias explaining all the different points which i found related to Geo-fencing, I would be trying out the 3 steps you have mentioned and will be updating the results after testing of my Demo/Sample.
  • Mathias
    Mathias over 9 years
    @Anuj glad i could help!
  • Leon
    Leon about 8 years
    "you'll always get a trigger immediately for a newly created geofence" This is not quite true, they only seem to trigger if you're already inside that fence.
  • Mathias
    Mathias about 8 years
    Leon, yes and i write that in the answer already. "if the device discovers that you're inside the geofence when creating it"
  • black
    black almost 7 years
    Setting immediate triggers don't work for exits either, only enters. Based on my test, the only time I get an immediate exit when outside of a created geofence is the first ever time of the app's lifetime on one device, and never on another device. It's a shame because there's a good chance you may be outside of the geofence you've just created, and there's no way of being sure because "outside" is completely based on google's algorithm. Waiting for an enter isn't an option either, specially now it Android 8, enters are gonna be delayed.
  • b3nk4n
    b3nk4n over 6 years
    "you'll always get a trigger immediately for a newly created geofence" I think this is only true when you define an INITIAL_TRIGGER, or keep it default which actually sets an INITIAL_TRIGGER_ENTER. But you could also call builder.setInitialTrigger(0) to get no event immediately after registering the geofence.
  • Michael Krause
    Michael Krause over 5 years
    To @Mathias' 2nd point about recreating geofences, there are several more situations where you will need to re-register. I've outlined all of these in my answer to a related post
  • Michael Krause
    Michael Krause over 5 years
    If you're specifying initial triggers, but not receiving them at all or inconsistently, this might be due to the radius of your geofence being to small. I've had good luck with a radius of at least 80m.