Search code examples
androidgpslocationlocationmanagerlocationlistener

One LocationListener in whole application


I need to implement one LocationListener in my entire application.

I know that I can use service with LocationListener, but the problem is that I have to use some notifier on location changed. It is not enough when I am able to get last location whenever I want, because I need current position in all Activities immediately when it changes.

So in pseudo code I need something like that:

myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {

public void onLocationChanged(Location location) {
                notifyAllActivities(location);
            }
};

and in every Activity:

void onNotify(Location location) { //... }


Solution

  • Make the service call sendBroadcast() to broadcast an Intent. Add the longitude/latitude as extras to that Intent. In each activity that needs the location, register a BroadcastReceiver for the intent and get the lng/lat from the Intent's extras in the receiver's onReceive()