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?
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: Android service with locationListener callbacks
精彩评论