17

I know BroadcastReceiver watches for text, phone events, etc... but can you run LocationServices as a service and event based on Location?

For example, you are near a certain GPS point and the phone notifies you.

ProNeticas
  • 986
  • 3
  • 10
  • 17

2 Answers2

37

I think what you are looking for is something like this. There is a version of LocationManager.requestLocationUpdates() that takes a PendingIntent as a parameter, and this Intent could be used to fire a BroadcastReceiver. This code will register a custom broadcast to fire with the location updates.

Intent intent = new Intent("UNIQUE_BROADCAST_ACTION_STRING_HERE");
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
long minTime;     //This should probably be fairly long
float minDistance; //This should probably be fairly big
String provider;   //GPS_PROVIDER or NETWORK_PROVIDER

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.requestLocationUpdates(provider, minTime, minDistance, launchIntent);

Then you just have to register your BroadcastReceiver with the same Intent action and it will receive the location updates for processing. And, of course, keep that PendingIntent around because you'll need to call manager.removeUpdates(launchIntent) at some point!

Final Note:

Because it sounds like you are trying to implement regular location updates while your app isn't in the foreground, keep this in mind. Unless you want your app branded as a battery killer, be extremely careful with how you implement this functionality.

You will want to greatly reduce the frequency up location updates with either a large minTime or minDistance parameter to allow the location service to rest. This feature should also be attached to a user controlled preference so they can enable/disable your app from tracking location in the background.

Hope that Helps!

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • @Wireless-Designs, you are a Gem, I thought this was the case, I am still learning about INTENT for the Droid, you response is very informative - thanks for your time kind sir! – ProNeticas Mar 09 '11 at 05:09
  • No problem. If this answer ends up solving your problem, feel free to accept it. Cheers! – devunwired Mar 09 '11 at 05:51
  • I will, I wanted to test first ;) – ProNeticas Mar 09 '11 at 06:50
  • Thanks for this. I was trying to do this with a service and the alert manager with no luck. This is exactly what i was looking for. – AaronSzy Mar 24 '11 at 22:15
22

This question is more then 2 years old but there is now very very easy and battery efficient way to do this. The concept is called Geofences. You can learn it in more detail below:

http://developer.android.com/training/location/geofencing.html

Niklas Ekman
  • 943
  • 1
  • 9
  • 26
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • So how do you propose this would work? Set a geofence for the user's current location for lets say 50m radius, then on exit event, set a new geofence with the same radius, and that way track a user's movement? – Pierre May 19 '18 at 22:01
  • @Pierre: if you want to update based on location change, have a look at https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html#setSmallestDisplacement(float) – serv-inc Jul 13 '18 at 11:31
  • 1
    Thanks @serv-inc, I have used extactly that. And to save battery power, I'm only logging locations with accuracy lower than 40m, I have also added a static variable called previous location, if the new location is farther than 40m from the previous location, I log it, else not. – Pierre Jul 14 '18 at 06:50