I want to get the current GPS coordinates using the GoogleApiClient
as the LocationClient
is now deprecated. But as I have more than one activity using the client, I don't want to repeatedly write an inner class for each activity.
But when I write an external class GPS
defining the localization functionality, I cannot give any feedback from onLocationChanged(Location location)
to the activities, which call the class GPS
. How is this usually done? How can I access the location
object from within the calling activity? Or is this only possible with an inner class?
I have written an app which uses geolocation accross most of its activities. A solution I have chosen to serve all my clients (e.g: activities, dialogs, or even other apps) was to write a Service
wrapping this logic of geolocation so I could bind/unbind from it as the user navigates between activities.
This also allowed me to keep the geolocation client up even when the user left all of the app activities, making it possible to notify him asynchronously of particular events happening nearby his location.
Your location logic will be located in one place in your project, and with a little bit of work abstracting away the kind of location provider you are using, you can test your app in a much more thorough way by changing the location provider with your own.