7

I just finished the tutorial for geofencing on Android (http://developer.android.com/training/location/geofencing.html) and I wonder why the 'callback' for geofences are done via pending intents and not a simple callback interface.

If implemented in an activity, one would usually disconnect the location client in onPause() anyway, so previously added geofences would not be tracked either after the application paused/was destroyed, so why a pending intent? Or am I mistaken here?

Flo
  • 1,469
  • 1
  • 18
  • 27

2 Answers2

18

I wonder why the 'callback' for geofences are done via pending intents and not a simple callback interface.

Mostly because geofences are designed to work even without your application running.

If implemented in an activity, one would usually disconnect the location client in onPause() anyway, so previously added geofences would not be tracked either after the application paused/was destroyed, so why a pending intent? Or am I mistaken here?

I believe that you are mistaken here. In fact, geofences specifically are not designed for directly triggering UI, as is discussed in the documentation:

The Intent sent from Location Services can trigger various actions in your app, but you should not have it start an activity or fragment, because components should only become visible in response to a user action.

Now, you might elect to say that you want to only use geofences while you have your activity in the foreground. However, you would have to remove those geofences in onPause(). A geofence will remain registered until its expiration time or manually removed, AFAICT.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Yep, what you said is absolutely correct - I just tried it out :) – Flo May 20 '13 at 04:56
  • 2
    In my testing, the sample code from Google stopped working if my app was killed. So I switched to a Broadcast Receiver instead of an IntentService and then the background monitoring worked as anticipated. http://stackoverflow.com/questions/19505614/android-geofence-eventually-stop-getting-transition-intents/19521823#19521823 – b-ryce Oct 24 '13 at 18:44
1

This answer can be outdated - accuracy and realiability of google play services has changed a lot from it's initial release.

Some of my experiences with geofencing below. First of all - the main advantage of this technology is VERY low battery usage. In the fact, I can't notice any changes in battery life. It's really impressive. Service seems to use only Wi-Fi and network location. I didn't notice GPS running at all. I can't say if it's only hidden location icon or really not using GPS. Accuracy - it's terrible. 20 circle areas are not detected at all, except range of my home ap. It looks like whole position circle, including error must be inside of fenced area. 1000m areas are detected sometimes and with huge latency. Those experiments where made in open area with very low number of Wi-Fi ap around. I'm still trying to find really reliable settings foot this service. After getting intents I want to turn on GPS location and make final approach in my own code.

piotrpo
  • 12,398
  • 7
  • 42
  • 58
  • what is "huge latency" like in your experience?? – Ewoks Jul 31 '14 at 23:51
  • Hi - As I wrote in note to my answer - a lot has changed - latency is much smaller. In typical urban area it works really good. – piotrpo Aug 06 '14 at 12:20
  • what is "much smaller" exactly? 1sec, 1min, 1h? – Ewoks Aug 06 '14 at 12:21
  • Hard to say "exactly". I have geofence set around my home with radius 200m - when I'm approaching on feet fence is always triggered. I think that latency in most cases does not exceed 1 minute, but it can differ a lot depending on situation - we must remember that this is designed as low power consumption service at the first. – piotrpo Aug 06 '14 at 12:28
  • 1
    i understand.. The problem is that something is wrong with my implementation (i guess some other apps as well) because sometimes not geofence triggers til I unlock phone, sometimes doesn't happen completely etc.. is your app open sourced? is it at least available in playstore? Do u use some different implementation than described in developers web site? 10q – Ewoks Aug 06 '14 at 13:02
  • Sorry - some commercial app - anyway - it's good enough for task like "you are near our shop" – piotrpo Aug 06 '14 at 13:41
  • Can u link the app in store so we can try it? – Ewoks Aug 07 '14 at 09:01