Android IntentService vs. Service
~ 2 min read. Published 19 May 2014 Android has often more than one way of doing something but it is not because of a poor design, rather because of the many possibilities that are available to Android developer and it is important to understand the nuances between various options to achieve something and choose the right tool for the job.
Let's look at services:
IntentService vs. Service
When to use?
- The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.
- The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).
How to trigger?
- The Service is triggered calling to method onStartService().
- The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.
Triggered From
- The Service may be triggered from any thread.
- The IntentService must be triggered from Main Thread.
Runs On
- The Service runs in background but it runs on the Main Thread of the application.
- The IntentService runs on a separate worker thread.
Limitations / Drawbacks
- The Service may block the Main Thread of the application.
- 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.