2

I am calling startService() multiple times in my class.

there is a function in my service's onStartCommand(), like this -

    Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(StaticValues.TAG, "service started.");
        processItem();
        return 0;
    }

My question is, if I start service again, onStartComamnd() will be called again. So will this call wait till my previous call is over or it will execute both calls to processItem() parallelly?

Edit : Answer I found from links in comments

startService() is asynchronous. So while you are looping the calls, the service itself hasn't gotten any resources and didn't start yet.

The Service will only run in one instance. However, everytime you start the service, the onStartCommand() method is called.

Check : What happens if a Service is started multiple times?

Community
  • 1
  • 1
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • [Check](http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29) – Skynet Jun 24 '15 at 11:20
  • 3
    onStartCommand is called in the UI thread so the answer is no: no parallelly – pskink Jun 24 '15 at 11:20
  • 1
    Possible duplicate of [What happens if I start a service multiple times?](http://stackoverflow.com/questions/8019899/what-happens-if-i-start-a-service-multiple-times) – Ciro Santilli OurBigBook.com Feb 14 '16 at 10:59

1 Answers1

1

A Service can only be started once, if you want to and love to complicate things use a boolean flag

Using startService() overrides the default service lifetime that is managed by bindService(Intent, ServiceConnection, int): it requires the service to remain running until stopService(Intent) is called, regardless of whether any clients are connected to it. Note that calls to startService() are not nesting: no matter how many times you call startService(), a single call to stopService(Intent) will stop it.

If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.

Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called;

Link to the Docs

Life Cycle of a Service

Skynet
  • 7,820
  • 5
  • 44
  • 80
  • But it is true that `onStartCommand()` will be called. So I can assume that `processItem()` function will be called atleast twice? – Darpan Jun 24 '15 at 11:25
  • It totally depends on when the call to `Context.stopService()` is called - which will nullify all the previous calls. – Skynet Jun 24 '15 at 11:26
  • Consider I never call stopService(), then? – Darpan Jun 24 '15 at 11:28
  • Please go through the Service Lifecycle, it also depends on your return type. Please also consider psKink's comment that a Service runs on the UI thread. You might instead want to use an `IntentService`. – Skynet Jun 24 '15 at 11:31
  • I figured it out, my function `processItem()` will be called each time as `onStartCommand()` is also getting called. – Darpan Jun 24 '15 at 13:05
  • I suggest you dont call it on the UI thread. It will cause potential problems in your app. – Skynet Jun 24 '15 at 13:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81432/discussion-between-darpan-and-skynet). – Darpan Jun 24 '15 at 15:18