Without seeing your code: a call to the method startService
will either start the service if it isn't running or call onStartCommand
if the service is already started. When a timer starts you could call startService
and increment a value(for example a int
value representing the live timers). When a timer needs to stop/finishes you could implement a BroadcastReceiver
in the Service
(like in the link you've shown) to listen for a "close" broadcast coming from other components(you could use this pattern to let the Service
know that a new timer has started(a "start" broadcast)).
In that BroadcastReceiver
from the Service
you would decrement the live timers count and see if you are at 0
, if this is the case then stop the Service
.
The main problems with the above approach is when to reliable close the Service
even if the other activities get killed and are not getting restarted so they can not broadcast the required timer closed intents(if the other activities get killed and are not restarted the service could be left with some registered timers and you wouldn't want the service to run indefinitely). To solve this it would require more details about your actual code.