Can you please help me understand what the difference between an IntentService
and a Service
is?
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
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.
IntentService
for your work –
Orvalorvan IntentService
cannot be bound from any component? –
Orvalorvan 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
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.
service
does not run in background but in the foreground. –
Perkin See Tejas Lagvankar's post about this subject. Below are some key differences between Service and IntentService and other components.
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.
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.
vs
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).
Service
: Works in the main thread so it will cause an ANR (Android Not Responding) after a few seconds.
IntentService
: 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
© 2022 - 2024 — McMap. All rights reserved.