Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple IntentService or one Service

I'm a little confused on the difference between IntentService and Service. I understand that IntentService has a worker queue, but is there any benefit to using multiple IntentService over one Service?

Here's an example of what I mean. Let's say I want my application to pull data from 3 sources. Should I start a service that runs three threads, each one pulling from their respective source? Or should I start three separate IntentServices?

like image 263
psoulos Avatar asked Jun 13 '11 17:06

psoulos


People also ask

What happens if we launch intent Service multiple times?

The IntentService stops the service after all requests have been handled, so you never have to call stopSelf() . The IntentService cannot run tasks in parallel and all the consecutive intents will go into the message queue and will execute sequentially.

What is the difference between IntentService and service?

If the task doesn't require any and also not a very long task you can use service. If the Background task is to be performed for a long time we can use the intent service. Service will always run on the main thread.

Can an IntentService execute multiple tasks sequentially?

The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.

Why is IntentService deprecated?

This constant was deprecated in API level 23. MODE_MULTI_PROCESS does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. Applications should not attempt to use it.


2 Answers

IntentService is just a convenient class to write services that are workers in the producer-consumer pattern. They are services designed to execute various tasks in a row and then stop. Services are not necessarily IntentServices such as services that must stay alive such as daemons.

So you should wonder if you service is close to a worker thread, if so, use IntentServices else just derive from Service.

Your second questions was whether to group all 3 services in a 3 in 1 service. The answer is that it depends how you use your datasources : if you use them altogether, then group them in a single service. If they are used separately, you could build a service for each with the hope to provide a lighter service if only one datasource is used and not the other. But if you use all 3 datasources, each in a service, then it will be heavier than using a single service.

like image 151
Snicolas Avatar answered Oct 21 '22 12:10

Snicolas


Its my understanding that the difference between intentService and Service is that an intentService will spawn a worker thread to run it, while a Service runs in the main thread of it's hosting process. In addition, an intentService will stop itself when the work is done, while a Service will continue running until stopSelf, or stopService is called.

If the 3 data sources need to share information with each other, then put them all in the same Service, otherwise keep them separate because if one data source is down it will leave a fat service running instead of just a single light Service.

like image 26
Chris Avatar answered Oct 21 '22 10:10

Chris