Can you please help me understand what the difference between an
IntentService and a
In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for “fire and forget” operations, taking care of background Thread creation and cleanup.
From the docs:
A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through
startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
Service is a base class of service implementation.
Service runs on the application’s main thread which may reduce the application performance. Thus,
IntentService, which is a direct subclass of Service is available to make things easier.
IntentService is used to perform a certain task in the background. Once done, the instance of
IntentService terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.
Serviceclass uses the application’s main thread, while
IntentServicecreates a worker thread and uses that thread to run the service.
IntentServicecreates a queue that passes one intent at a time to
onHandleIntent(). Thus, implementing a multi-thread should be made by extending
Serviceclass needs a manual stop using
IntentServiceautomatically stops itself when it finishes execution.
null. This means that the
IntentServicecan not be bound by default.
onStartCommand()that sends Intent to queue and to
In brief, there are only two things to do to use
IntentService. Firstly, to implement the constructor. And secondly, to implement
onHandleIntent(). For other callback methods, the super is needed to be called so that it can be tracked properly.
service: It runs in the background on your system. For example,
- If you went to a hotel and you give your order for a soup to a server
- The server gets your order and sends to chef
- You don’t know how the soup is made in the kitchen and what processes are required for making the soup
- Once your order is ready, the server brings you the soup.
background process: chef making soup
IntentService:- it’s consecutive service.. (i.e) when you order many food items at a time to server but the server delivers those items one by one and not deliver them all at once.
See Tejas Lagvankar’s post about this subject.
Below are some key differences between Service and IntentService and other components.
Service runs actually in the same thread of your app; when you extends Service, you must manually spawn new threads to run CPU blocking operations.
IntentService is a subclass of
Service which spawns a thread to do background work from there(No need to create a new thread to do CPU blocking operations).
- Task with no UI,but should not use for long Task. Use Thread within service for long Task
- Invoke by
- Triggered from any Thread
- Runs On Main Thread
- May block
- Long task usually no communication with main thread if communication is needed then it is done by Handler or broadcast
- Invoke via Intent
- triggered from Main Thread (Intent is received on main Thread and worker thread is spawned)
- Runs on separate thread
- We can’t run task in parallel and multiple intents are Queued on the same worker thread.
Service: Works in the main thread so it will cause an ANR (Android Not Responding) after a few seconds.
Service with another background thread working separately to do something without interacting with the main thread.
Intent service is child of Service
IntentService: If you want to download a bunch of images at the start of opening your app. It’s a one-time process and can clean itself up once everything is downloaded.
Service: A Service which will constantly be used to communicate between your app and back-end with web API calls. Even if it is finished with its current task, you still want it to be around a few minutes later, for more communication