What is the difference between an IntentService and a Service? [duplicate]
Asked Answered
T

8

143

Can you please help me understand what the difference between an IntentService and a Service is?

Turnery answered 14/10, 2011 at 17:28 Comment(4)
techtej.blogspot.com.es/2011/03/…Basilio
Really good comparison here: https://mcmap.net/q/53728/-service-vs-intentservice-in-the-android-platformGladiate
Please go to the above links they are really TOO GOOD.. very big thanks for them...Bolide
How is an older question a duplicate of a newer one?Druce
V
125

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

Valois answered 14/10, 2011 at 17:55 Comment(4)
IntentService is not base class of Services. Reverse of it is true. Check - developer.android.com/reference/android/app/IntentService.htmlCommutable
@Commutable It is a base class for "Services that handle asynchronous requests", not for simply "Services". That's why the sentence above is correct.Billie
Hi all, can i write an IntentService within a Service? If yes, can anyone please provide an example/code snippet. ThanksDisentangle
@Valois i am making an app in which i need to scan the local database in every half an hour which would be better IntentService or Service ??Corves
C
131

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

  1. Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service.
  2. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly. Service class needs a manual stop using stopSelf(). Meanwhile, IntentService automatically stops itself when it finishes execution.
  3. IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.
  4. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent().

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.

Cassandra answered 28/12, 2012 at 13:43 Comment(7)
Nice Explaination. I am making Music app. So, which one be nice for it? Service or IntentService ?Guiana
for music app i think you have to use Service because, as default, IntentService is unbinddable and, i think, you have to bind your music app with music service to start, to stop. As already said, IntentService is better for "start and forget"Lorikeet
@Lorikeet Wouldn't IntentService be similar to Service.START_NOT_STICKY? As that is also "start and forget" according to your example.Alphorn
i have an app which can sync data after some time through service so what should i use Service or Intent Service ?Nightshade
@Nightshade Syncing data can result in long running operation, so i think you should use IntentService for your workOrvalorvan
@Jashan PJ Can you explain why IntentService cannot be bound from any component?Orvalorvan
Are you sure Service runs on main thread? I can create and run it in another oneWhaley
V
125

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

Valois answered 14/10, 2011 at 17:55 Comment(4)
IntentService is not base class of Services. Reverse of it is true. Check - developer.android.com/reference/android/app/IntentService.htmlCommutable
@Commutable It is a base class for "Services that handle asynchronous requests", not for simply "Services". That's why the sentence above is correct.Billie
Hi all, can i write an IntentService within a Service? If yes, can anyone please provide an example/code snippet. ThanksDisentangle
@Valois i am making an app in which i need to scan the local database in every half an hour which would be better IntentService or Service ??Corves
I
71

service: It runs in the background on your system. For example,

  1. If you went to a hotel and you give your order for a soup to a server
  2. The server gets your order and sends to chef
  3. You don't know how the soup is made in the kitchen and what processes are required for making the soup
  4. 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.

Iaea answered 17/2, 2012 at 5:16 Comment(5)
I like your style of explaining.Scarlet
Sorry , but it is missing important differences between the two..Potamic
What do you mean by "consecutive service"?Druce
This explanation is incorrect. A service does not run in background but in the foreground.Perkin
Explanation is impressive.Lent
B
61

See Tejas Lagvankar's post about this subject. Below are some key differences between Service and IntentService and other components.

enter image description here

Bloodline answered 1/1, 2016 at 5:49 Comment(1)
nice tabular informationYoshida
A
18

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.
Afreet answered 19/5, 2014 at 4:46 Comment(0)
G
17

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).

Grecian answered 6/10, 2012 at 12:2 Comment(2)
isn't it to prevent CPU blocking operations, not run them? Im not sure thoughKagu
thats true, service neew a new thread to run CPU blocking operationsBenford
P
4

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.

Paestum answered 9/10, 2013 at 13:9 Comment(1)
Service: Works in the main thread so it will cause an ANR (Android Not Responding) after a few seconds. - I can have a service running for hours and hours and I've never seen an ANR, so I doubt that is true.Vday
R
2

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

Reject answered 5/12, 2013 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.