Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Why use pending intents for geofences

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?

like image 525
Flo Avatar asked May 19 '13 02:05

Flo


People also ask

Why do we set pending intent in notification?

Android PendingIntent A PendingIntent is generally used in cases were an AlarmManager needs to be executed or for Notification (that we'll implement later in this tutorial). A PendingIntent provides a means for applications to work, even after their process exits.

What is the use of pending intent in android?

A Pending Intent specifies an action to take in the future. It lets you pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as your application, whether or not your application is still around when the Intent is eventually invoked.

What is the recommended radius for a geofence?

For best results, the minimum radius of the geofence should be set between 100 - 150 meters.

What is the difference between intent and PendingIntent?

In conclusion, the general and main difference between Intent and PendingIntent is that by using the first, you want to start / launch / execute something NOW, while by using the second entity you want to execute that something in the future.


2 Answers

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.

like image 109
CommonsWare Avatar answered Sep 24 '22 16:09

CommonsWare


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.

like image 26
piotrpo Avatar answered Sep 21 '22 16:09

piotrpo