BroadcastReceiver for location
Asked Answered
S

2

18

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.

Stein answered 9/3, 2011 at 1:0 Comment(1)
Here you go developer.android.com/reference/android/location/…Corrody
P
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!

Plumbaginaceous answered 9/3, 2011 at 2:52 Comment(4)
@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!Stein
No problem. If this answer ends up solving your problem, feel free to accept it. Cheers!Plumbaginaceous
I will, I wanted to test first ;)Stein
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.Placencia
C
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

Chessa answered 1/8, 2013 at 11:44 Comment(4)
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?Bantu
@Pierre: if you want to update based on location change, have a look at developers.google.com/android/reference/com/google/android/gms/…Mournful
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.Bantu
Note that the this Geofencing api requires play services, as opposed to LocationManager which comes with AOSP.Rey

© 2022 - 2024 — McMap. All rights reserved.