Home » Android » What happens if a Android Service is started multiple times?

What happens if a Android Service is started multiple times?

Posted by: admin March 11, 2020 Leave a comment

Questions:

If I have the following code:

Intent intent = new Intent(this,DownloadService.class);     
for(int i=0;i<filesArray.length;i++){        
     startService(intent);          
}

In this code DownloadService extends IntentService.

So now when I’m calling startService(intent) does that mean that I’m starting a new service every time startService(intent) is called or does that mean that DownloadService is run once and then each time I call startService(intent) it will just pass a different intent with a different startId.

Does this make sense, and which one of these is the case ?

How to&Answers:

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

This is documented here

Answer:

Absolutely Correct.
Only one instance of Service is created for an application process.
And when you call StartService(); again, then only onStartCommand() gets called and new Intent is passed to onStartCommand() method.

Note: onCreate() is not called again.

About calling bindService() multiple times:

When you call bindService() multiple times, then again only one instance is used for Service and Android Runtime returns same IBinder object to client.

Means, onBind() is not called multiple times. And just cached IBinder object is returned.

Answer:

Adding some more information to the above answers which may be helpful for others is that, startId that the onStartCommand() method receives is different for every startService() call.

Also if we write in for loop as mentioned above, code written in onHandleIntent() would execute so many times as defined by the for loop frequency, but in sequence and not in parallel.

The concept is IntentService creates a work queue and each request to startService() trigger onStartCommand() which in turn stores the intent in work queue and then pass the intent one by one to onHandleIntent().

Answer:

According to the doc:

The startService() method returns immediately, and the Android system calls the service’s onStartCommand() method. If the service isn’t already running, the system first calls onCreate(), and then it calls onStartCommand().

and

Multiple requests to start the service result in multiple corresponding calls to the service’s onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.