Android - Best way to implement LocationListener across multiple activities?

The way that I would typically implement this requirement is using a bound Service implementation, like the one in the Local Service Sample in the SDK Documentation. Obviously you're familiar with the advantage of the Service allowing you to create all the location code only once.

The way that I would typically implement this requirement is using a bound Service implementation, like the one in the Local Service Sample in the SDK Documentation. Obviously you're familiar with the advantage of the Service allowing you to create all the location code only once. Accessing the Service through Bindings allows the Service to start and stop itself so it isn't running when your application isn't in the foreground (it will die as soon as no more Activities are bound).

The key, IMO, to making this work well is to BIND the service in onStart() and UNBIND in onStop(), because those two calls overlap as you move from one Activity to another (Second Activity Starts before the First one Stops). This keeps the Service from dying when moving around inside the app, and only lets the service die when the entire application (or at least any part interested in location) leaves the foreground. With Bindings, you don't have to pass the Location data in a Broadcast, because the Activity can call methods directly on the Service to get the latest location.

However, a Broadcast would still be advantageous as a method of indicating WHEN a new update is available...but this would just become a notifier to the listening Activity to call the getLocation() method on the Service. My $0.02. Hope that Helps!

I have been down this road before but I didn't know that onStart and onStop overlapped like that. This is appealing to me and I will try this out and let you know what I come up with tomorrow. Thanks!

– d370urn3ur Apr 25 '11 at 22:28 Just wondering, how would this work with turning off the screen? Would it be wise to BIND the service on onResume() and UNBIND on onPause() to turn it on/off when the screen turns on/off as well as working when activities switch? – Grantland Chew Jun 23 '11 at 19:29 @Grantland Valid point, since the Activity is still foreground the Service would live.

If the service needs to throttle itself back in these instances, I would keep the binding the same, but have it register a BroadcastReceiver for Intent. ACTION_SCREEN_ON and Intent. ACTION_SCREEN_OFF to turn off heavy components like GPS.

Moving the binding mechanism into onPause()/onResume() causes the service to possibly stop/restart often throughout use of the application, since those callbacks never overlap, letting the service fall into an unbound state frequently. – Devunwired Sep 8 '11 at 18:48.

You could have your service writing your lat & long coords to an sqlite db on change that way you don't have the service binding overhead and your coords will be accessible across all your activities. Perhaps in your main activity you can check the GPS status and if it is turned off you can prompt the user to enable it. Once GPS is enabled start your service.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions