views:

26

answers:

1

Hi,

I know that we can get the current location of Android device using getLastKnownLocation and by registering a listener in requestForUpdates method. But, this method works for an Activity which I think means that the updates will be sent to that particular activity only.

I want to receive the location update in any activity. It should be registered when the application is started and any visible activity should be able to get the current location.

Do I need to then make a different class and extend it with Service class and start the service when the application is started? Or Is there any other way around?

Also, can someone let me know what will happen if two different activities registers for requestForUpdate method. Will the first one be automatically unregistered? or they both will receive the updates?

Also, I am not sure but I think whenever the activity is destroyed the location listener are automatically unregistered or they need to be unregistered in the onDestroy method of activity?

Does someone have any idea about all this?

A: 

Create a Service that checks getLastKnownLocation and then sends a broadcast to all interested Activities.

If you want to do this periodically, set an Alarm.

See this post for details: http://stackoverflow.com/questions/3743474/android-service-with-locationlistener-callbacks

Peter Knego