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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With