views:

52

answers:

1

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, if users keep the application in the background, the Service never stops and drains battery.

I would like the Service to stop when my application is not in the foreground. I was hoping the Application class would let me Override onPause and onResume handlers, but it does not have them. Is there another way I can accomplish this?

A: 

You should override the onPause and onResume methods on your Activity. If you have multiple activities you may want to have a common base class for them and put the start/stop logic into the base class.

fornwall
So you want my location Service to stop and start every time I go to a new screen? I don't think that's what I'm looking for.
Andrew
You could set a flag when launching an activity within your own application, and only stop your service if the launching-own-activity flag is not set.
fornwall