Friday, 9 August 2013

How to implement a service that runs only when the app is running?

How to implement a service that runs only when the app is running?

The app has a service which has to detect how many minutes the app is
running and based on that, the service will initiate misc actions.
What is the proper way to implement this?
How can I be sure the service is running ONLY when the app is running in
front of the user?
Starting the service seems easy - just start it on splash loading. But the
harder part is ending it. I cannot just end it when the user press Back
button on the last screen. How to handle situation when a user presses
Home screen or some other other app (like phone call, or viber popup,
or...) takes over the screen?
I tried taking suggestions from the other theme (How to start a android
service from one activity and stop service in another activity?), but this
does not handle the situation with Home button or other app taking over
the screen.
The app has in total around 10 activities. Is it a proper way to bind this
service to all 10 activities and when all are off, the service then turn
itself off?

No comments:

Post a Comment