1

This is just a knowledge question, I know it's not a good practice to do so. So here it is:

If I call startService() 3 times while it is already running, do I have to call stopService() 3 times to make it stop? (Does it have a stack of waiting intent?) Or just once will kill it for good?

Jaythaking
  • 2,200
  • 4
  • 25
  • 67

1 Answers1

5

If I start a Service 3 times while it is already running, do I have to call stopService 3 times to make it stop?

No. One stopService() is sufficient.

I know it's not a good practice to do so

Sure it is. If desired, your activity (or whatever is calling startService()) can blindly call startService() without knowing or caring whether the service is up and running. For example, if you are implementing an IntentService for handling downloads of mid-sized files, the activity can call startService() for as many downloads as the user wants. Note though that IntentService stops itself when it has no more work to do.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Hey, what is about we checking running services before starting service ,like my answer :http://stackoverflow.com/questions/37469409/does-calling-stopservice-stops-all-awaiting-service-intent/37469513#37469513 – Yasin Kaçmaz May 26 '16 at 20:53
  • 1
    @YasinKaçmaz: Generally, I try to avoid that pattern. – CommonsWare May 26 '16 at 21:11
  • why you avoid , is there any special reason. I am using this pattern in my app and never getting error ? – Yasin Kaçmaz May 26 '16 at 21:12
  • 3
    @YasinKaçmaz: If the activity wants the service to be started, it calls `startService()`. If the activity wants to know information about the state of the service, use a bound service and get the status from the `Binder`, or use an event bus. I have yet to encounter a situation where the activity *only* needs to know that the service is started (which is all your approach can determine) *and* cannot just call `startService()`. You are welcome to do what you want; I just avoid your approach. – CommonsWare May 26 '16 at 21:24
  • I understand , i am using eventbus too but in my service i just doing web call to update location etc. I dont need to determine situations , but like your approach. Maybe next times i need to do that. Thank you . I will favourite this question. – Yasin Kaçmaz May 26 '16 at 21:28