bytebiscuit bytebiscuit - 5 months ago 11
Android Question

What happens if a Service is started multiple times?

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 ?

Answer

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

This is documented here

Comments