I have a Service which tracks the location of the user. Currently, the Service boots when the application starts and stops when the application terminates. Unfortunately,
I haven't tested this yet, but it looks like if you use Context#bindService()
(instead of Context#startService()
), the service should stop when no more activities are bound to it. (see Service lifecycle).
Then use onPause()
/onResume()
in each activity to bind/unbind from the service.
Alternatively, you could add a pair of methods on your service which tell it to start/stop listening for location updates and call it from each activity's onResume()
/onPause()
. The service would still be running, but the location updates wouldn't be draining the battery.