What is the difference between an IntentService and a Service? [duplicate]
Solution 1:
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:
Service 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.
IntentService
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.
Refer this doc - http://developer.android.com/reference/android/app/IntentService.html
Solution 2:
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.
The 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.
Differences
-
Service
class uses the application's main thread, whileIntentService
creates a worker thread and uses that thread to run the service. -
IntentService
creates a queue that passes one intent at a time toonHandleIntent()
. Thus, implementing a multi-thread should be made by extendingService
class directly.Service
class needs a manual stop usingstopSelf()
. Meanwhile,IntentService
automatically stops itself when it finishes execution. -
IntentService
implementsonBind()
that returnsnull
. This means that theIntentService
can not be bound by default. -
IntentService
implementsonStartCommand()
that sends Intent to queue and toonHandleIntent()
.
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.
Solution 3:
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.
Solution 4:
See Tejas Lagvankar's post about this subject. Below are some key differences between Service and IntentService and other components.
Solution 5:
Service
- Task with no UI,but should not use for long Task. Use Thread within service for long Task
- Invoke by
onStartService()
- Triggered from any Thread
- Runs On Main Thread
- May block
main(UI
) thread
IntentService
- 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.