0

I use ActionBar tabs with multiple fragments. Each fragment contains a timer based on my timer service. When I stop my first timer (stopping the timer-service) the second timer stops aswell, I guess that's because they are running the same service.

Is it possible to distiguish if another fragment is using the service or maybe to start individual services for each timer? Or is there another way of doing it?

I have been looking for a solution for some time now, I'm a bit lost right now.

aelveborn
  • 357
  • 1
  • 4
  • 17
  • What do you mean "timer"? Do you mean the Java `Timer` class or some generic functionality that acts as a timer? – DeeV Aug 10 '12 at 12:20
  • 1
    Services can only have a single instance. You are not able to start multiple instances of the same service. Is the timer only required while the activities are in use or are they also required in the background? – Phil H Aug 10 '12 at 12:22
  • @DeeV The service uses a broadcastservice and a runnable that notifys my fragmants to update ui every second. This way I can have my timers running even if the phone is not in use (onPause releases the bradcastservice). – aelveborn Aug 10 '12 at 12:28
  • @PhilH the service is requierd in the background if the user has started a timer (it's a foreground service). – aelveborn Aug 10 '12 at 12:28
  • Every time a timer is started register it with the service so the service will know how many timers are out there. When one of the timers needs to stop then remove it from the service's list and if the service doesn't have any more timers registered kill it. If it does simple carry on counting. – user Aug 10 '12 at 12:35
  • @Luksprog that sounds like a good solution. Could you kick me in the right direction on how to accomplish that? – aelveborn Aug 10 '12 at 12:38
  • @Luksprog is it something like this [link](http://stackoverflow.com/a/5101790/1168301)? That way I can update an int with number of activitys using the service. – aelveborn Aug 10 '12 at 13:04

1 Answers1

0

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.

user
  • 86,916
  • 18
  • 197
  • 190
  • Hi and thanks for the answer, I have implemented it like you said above. I do have had issues with the service still running, if I for example press the backbutton. I will update with some code tomorrow. – aelveborn Aug 11 '12 at 02:31