This question already has answers here:
Service vs IntentService in the Android platform
(11 answers)
Closed 6 years ago.
Can you please help me understand what the difference between an IntentService and a Service is?
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, while IntentService creates a worker thread and uses that thread to run the service.
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.
IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.
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.
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.
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
Related
This question already has answers here:
Service vs IntentService in the Android platform
(11 answers)
Closed 5 years ago.
What is the difference between Service and an IntentService in Android?
What is the difference between AsyncTask and an IntentService in Android?
1. Difference between Service and IntentService
Service: It is the base class for the Android services, that you can extend for creating any service.
Since the service run inside the UI thread, it requires that you create a working thread for executing its work.
IntentService: it is a subclass of Service, that simplifies your work. It works already in a working thread, and can receive asynchronous requests. So, you don't need to create it manually, or to worry about synchronization. You can simply extend it and override the method:
onHandleIntent(Intent intent)
where you can manage all the incoming requests.
Taking a look at the documentation, you can see in details what the IntentService do for you:
Creates a default worker thread that executes all intents delivered to onStartCommand() separate from your application's main thread.
Creates a work queue that passes one intent at a time to your onHandleIntent() implementation, so you never have to worry about multi-threading.
Stops the service after all start requests have been handled, so you never have to call stopSelf().
Provides default implementation of onBind() that returns null.
Provides a default implementation of onStartCommand() that sends the intent to the work queue and then to your onHandleIntent() implementation.
So, if you need more control you can use the Service class, but often for a simple service the best solution is the IntentService.
2. Difference between AsyncTask and Service
They are two different concepts.
Service: can be intended as an Activity with no interface. It is suitable for long-running operations.
AsyncTask: is a particular class that wraps a working thread (performing background operations), facilitating the interaction with the UI Thread, without managing threads or handlers directly.
This question already has answers here:
Service vs IntentService in the Android platform
(11 answers)
Closed 7 years ago.
I am building an Android app, and I need to check a BBDD value periodically, approximately every 60 seconds. When app finds it, a value will be sent to app to update data values in UI.
My app has an Activity called MainActivity, and changes the fragments with NavigationDrawer and the begintransaction method from supportFragmentManager.
What to use in this case?
Check out the following post
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 by calling method startService().
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 and IntentService may be triggered from any thread, activity or other application component.
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.
When to stop?
If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).
The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().
I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?
I also believe that an IntentService runs in a different thread and a Service does not. So, as far as I can see, starting a service within its own thread is like starting an IntentService. Is that correct?
Tejas Lagvankar wrote a nice post about this subject.
Below are some key differences between Service and IntentService.
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 by calling method startService().
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 and IntentService may be triggered from any thread, activity or other application component.
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.
When to stop?
If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).
The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().
If someone can show me an example of something that can be done with an IntentService and can not be done with a Service and the other way around.
By definition, that is impossible. IntentService is a subclass of Service, written in Java. Hence, anything an IntentService does, a Service could do, by including the relevant bits of code that IntentService uses.
Starting a service with its own thread is like starting an IntentService. Is it not?
The three primary features of an IntentService are:
the background thread
the automatic queuing of Intents delivered to onStartCommand(), so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn
the automatic shutdown of the IntentService, via a call to stopSelf(), once the queue is empty
Any and all of that could be implemented by a Service without extending IntentService.
Service
Invoke by startService()
Triggered from any Thread
Runs on Main Thread
May block main (UI) thread. Always use thread within service for long task
Once task has done, it is our responsibility to stop service by calling stopSelf() or stopService()
IntentService
It performs long task usually no communication with main thread if communication is needed then it is done by Handler or BroadcastReceiver
Invoke via Intent
Triggered from Main Thread
Runs on the separate thread
Can't run the task in parallel and multiple intents are Queued on the same worker thread.
Don't reinvent the wheel
IntentService extends Service class which clearly means that IntentService is intentionally made for same purpose.
So what is the purpose ?
`IntentService's purpose is to make our job easier to run background tasks without even worrying about
Creation of worker thread
Queuing the processing multiple-request one by one (Threading)
Destroying the Service
So NO, Service can do any task which an IntentService would do. If your requirements fall under the above-mentioned criteria, then you don't have to write those logics in the Service class.
So don't reinvent the wheel because IntentService is the invented wheel.
The "Main" difference
The Service runs on the UI thread while an IntentService runs on a
separate thread
When do you use IntentService?
When you want to perform multiple background tasks one by one which exists beyond the scope of an Activity then the IntentService is perfect.
How IntentService is made from Service
A normal service runs on the UI Thread(Any Android Component type runs on UI thread by default eg Activity, BroadcastReceiver, ContentProvider and Service). If you have to do some work that may take a while to complete then you have to create a thread. In the case of multiple requests, you will have to deal with synchronization.
IntentService is given some default implementation which does those tasks for you.
According to developer page
IntentService creates a Worker Thread
IntentService creates a Work Queue which sends request to onHandleIntent() method one by one
When there is no work then IntentService calls stopSelf() method
Provides default implementation for onBind() method which is null
Default implementation for onStartCommand() which sends Intent request to WorkQueue and eventually to onHandleIntent()
Adding points to the accepted answer:
See the usage of IntentService within Android API.
eg:
public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");
}
#Override
protected void onHandleIntent(Intent intent) { ...}
To create an IntentService component for your app, define a class that extends IntentService, and within it, define a method that overrides onHandleIntent().
Also, see the source code of the IntentService, it's constructor and life cycle methods like onStartCommand...
#Override
public int More ...onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
Service together an AsyncTask is one of best approaches for many
use cases where the payload is not huge. or just create a class
extending IntentSerivce. From Android version 4.0 all network
operations should be in background process otherwise the application compile/build fails.
separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. For more discussion of this topic, see the blog post
from Android developers guide:
IntentService is a base class for 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.
Design pattern used in IntentService
:
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask.
An IntentService has a few limitations:
It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
An operation running on an IntentService can't be interrupted.
However, in most cases
IntentService is the preferred way to simple background operations
**
Volley Library
There is the library called volley-library for developing android networking applications
The source code is available for the public in GitHub.
The android official documentation for Best practices for Background jobs:
helps better understand on intent service, thread, handler, service. and also Performing Network Operations
I'm sure you can find an extensive list of differences by simply googling something such as 'Android IntentService vs Service'
One of the more important differences per example is that IntentService ends itself once it's done.
Some examples (quickly made up) could be;
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.
IntentService
IntentService runs on its own thread.
It will stop itself when it's done. More like fire and forget.
Subsequent calls will be queued. Good for queuing calls.
You can also spin multiple threads within IntentServiceif you need to- You can achieve this using ThreadPoolExecutor.
I say this because many people asked me "why use IntentService since it doesn't support parallel execution".
IntentService is just a thread. You can do whatever you need inside it- Even spinning multiple threads. The only caveat is that IntentService finishes as soon as you spin those multiple threads. It doesn't wait for those threads to come back. You need to take care of this. So I recommend using ThreadPoolExecutor in those scenarios.
Good for Syncing, uploading etc …
Service
By Default Service runs on the main thread. You need to spin a worker thread to do your job.
You need to stop service explicitly.
I used it for a situation when you need to run stuff in the background even when you move away from your app and come back more for a Headless service.
Again you can run multiple threads if you need to.
Can be used for apps like music players.
You can always communicate back to your activity using BroadcastReceivers if you need to.
An IntentService is an extension of a Service that is made to ease the execution of a task that needs to be executed in background and in a seperated thread.
IntentService starts, create a thread and runs its task in the thread. once done, it cleans everything. Only one instance of a IntentService can run at the same time, several calls are enqueued.
It is very simple to use and very convenient for a lot of uses, for instance downloading stuff. But it has limitations that can make you want to use instead the more basic (not simple) Service.
For example, a service connected to a xmpp server and bound by activities cannot be simply done using an IntentService. You'll end up ignoring or overriding IntentService stuffs.
Android IntentService vs Service
1.Service
A Service is invoked using startService().
A Service can be invoked from any thread.
A Service runs background operations on the Main Thread of the
Application by default. Hence it can block your Application’s UI.
A Service invoked multiple times would create multiple instances.
A service needs to be stopped using stopSelf() or stopService().
Android service can run parallel operations.
2. IntentService
An IntentService is invoked using Intent.
An IntentService can in invoked from the Main thread only.
An IntentService creates a separate worker thread to run background
operations.
An IntentService invoked multiple times won’t create multiple
instances.
An IntentService automatically stops after the queue is completed. No
need to trigger stopService() or stopSelf().
In an IntentService, multiple intent calls are automatically Queued
and they would be executed sequentially.
An IntentService cannot run parallel operation like a Service.
Refer from Here
If someone can show me an example of something that you can be done with an IntentService and can not be done with a service and the other way around.
IntentService can not be used for Long Time Listening, Like for XMPP Listeners, its a single time operator, do the job and wave goodbye.
Also it has just one threadworker, but with a trick, you can use it as unlimited.
The Major Difference between a Service and an IntentService is described as follows:
Service :
1.A Service by default, runs on the application's main thread.(here no default worker thread is available).So the user needs to create a separate thread and do the required work in that thread.
2.Allows Multiple requests at a time.(Multi Threading)
IntentService :
1.Now, coming to IntentService, here a default worker thread is available to perform any operation. Note that - You need to implement onHandleIntent() method ,which receives the intent for each start request, where you can do the background work.
2.But it allows only one request at a time.
I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?
I also believe that an IntentService runs in a different thread and a Service does not. So, as far as I can see, starting a service within its own thread is like starting an IntentService. Is that correct?
Tejas Lagvankar wrote a nice post about this subject.
Below are some key differences between Service and IntentService.
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 by calling method startService().
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 and IntentService may be triggered from any thread, activity or other application component.
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.
When to stop?
If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).
The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().
If someone can show me an example of something that can be done with an IntentService and can not be done with a Service and the other way around.
By definition, that is impossible. IntentService is a subclass of Service, written in Java. Hence, anything an IntentService does, a Service could do, by including the relevant bits of code that IntentService uses.
Starting a service with its own thread is like starting an IntentService. Is it not?
The three primary features of an IntentService are:
the background thread
the automatic queuing of Intents delivered to onStartCommand(), so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn
the automatic shutdown of the IntentService, via a call to stopSelf(), once the queue is empty
Any and all of that could be implemented by a Service without extending IntentService.
Service
Invoke by startService()
Triggered from any Thread
Runs on Main Thread
May block main (UI) thread. Always use thread within service for long task
Once task has done, it is our responsibility to stop service by calling stopSelf() or stopService()
IntentService
It performs long task usually no communication with main thread if communication is needed then it is done by Handler or BroadcastReceiver
Invoke via Intent
Triggered from Main Thread
Runs on the separate thread
Can't run the task in parallel and multiple intents are Queued on the same worker thread.
Don't reinvent the wheel
IntentService extends Service class which clearly means that IntentService is intentionally made for same purpose.
So what is the purpose ?
`IntentService's purpose is to make our job easier to run background tasks without even worrying about
Creation of worker thread
Queuing the processing multiple-request one by one (Threading)
Destroying the Service
So NO, Service can do any task which an IntentService would do. If your requirements fall under the above-mentioned criteria, then you don't have to write those logics in the Service class.
So don't reinvent the wheel because IntentService is the invented wheel.
The "Main" difference
The Service runs on the UI thread while an IntentService runs on a
separate thread
When do you use IntentService?
When you want to perform multiple background tasks one by one which exists beyond the scope of an Activity then the IntentService is perfect.
How IntentService is made from Service
A normal service runs on the UI Thread(Any Android Component type runs on UI thread by default eg Activity, BroadcastReceiver, ContentProvider and Service). If you have to do some work that may take a while to complete then you have to create a thread. In the case of multiple requests, you will have to deal with synchronization.
IntentService is given some default implementation which does those tasks for you.
According to developer page
IntentService creates a Worker Thread
IntentService creates a Work Queue which sends request to onHandleIntent() method one by one
When there is no work then IntentService calls stopSelf() method
Provides default implementation for onBind() method which is null
Default implementation for onStartCommand() which sends Intent request to WorkQueue and eventually to onHandleIntent()
Adding points to the accepted answer:
See the usage of IntentService within Android API.
eg:
public class SimpleWakefulService extends IntentService {
public SimpleWakefulService() {
super("SimpleWakefulService");
}
#Override
protected void onHandleIntent(Intent intent) { ...}
To create an IntentService component for your app, define a class that extends IntentService, and within it, define a method that overrides onHandleIntent().
Also, see the source code of the IntentService, it's constructor and life cycle methods like onStartCommand...
#Override
public int More ...onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
Service together an AsyncTask is one of best approaches for many
use cases where the payload is not huge. or just create a class
extending IntentSerivce. From Android version 4.0 all network
operations should be in background process otherwise the application compile/build fails.
separate thread from the UI. The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. For more discussion of this topic, see the blog post
from Android developers guide:
IntentService is a base class for 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.
Design pattern used in IntentService
:
This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask.
An IntentService has a few limitations:
It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.
Work requests run sequentially. If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished.
An operation running on an IntentService can't be interrupted.
However, in most cases
IntentService is the preferred way to simple background operations
**
Volley Library
There is the library called volley-library for developing android networking applications
The source code is available for the public in GitHub.
The android official documentation for Best practices for Background jobs:
helps better understand on intent service, thread, handler, service. and also Performing Network Operations
I'm sure you can find an extensive list of differences by simply googling something such as 'Android IntentService vs Service'
One of the more important differences per example is that IntentService ends itself once it's done.
Some examples (quickly made up) could be;
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.
IntentService
IntentService runs on its own thread.
It will stop itself when it's done. More like fire and forget.
Subsequent calls will be queued. Good for queuing calls.
You can also spin multiple threads within IntentServiceif you need to- You can achieve this using ThreadPoolExecutor.
I say this because many people asked me "why use IntentService since it doesn't support parallel execution".
IntentService is just a thread. You can do whatever you need inside it- Even spinning multiple threads. The only caveat is that IntentService finishes as soon as you spin those multiple threads. It doesn't wait for those threads to come back. You need to take care of this. So I recommend using ThreadPoolExecutor in those scenarios.
Good for Syncing, uploading etc …
Service
By Default Service runs on the main thread. You need to spin a worker thread to do your job.
You need to stop service explicitly.
I used it for a situation when you need to run stuff in the background even when you move away from your app and come back more for a Headless service.
Again you can run multiple threads if you need to.
Can be used for apps like music players.
You can always communicate back to your activity using BroadcastReceivers if you need to.
An IntentService is an extension of a Service that is made to ease the execution of a task that needs to be executed in background and in a seperated thread.
IntentService starts, create a thread and runs its task in the thread. once done, it cleans everything. Only one instance of a IntentService can run at the same time, several calls are enqueued.
It is very simple to use and very convenient for a lot of uses, for instance downloading stuff. But it has limitations that can make you want to use instead the more basic (not simple) Service.
For example, a service connected to a xmpp server and bound by activities cannot be simply done using an IntentService. You'll end up ignoring or overriding IntentService stuffs.
Android IntentService vs Service
1.Service
A Service is invoked using startService().
A Service can be invoked from any thread.
A Service runs background operations on the Main Thread of the
Application by default. Hence it can block your Application’s UI.
A Service invoked multiple times would create multiple instances.
A service needs to be stopped using stopSelf() or stopService().
Android service can run parallel operations.
2. IntentService
An IntentService is invoked using Intent.
An IntentService can in invoked from the Main thread only.
An IntentService creates a separate worker thread to run background
operations.
An IntentService invoked multiple times won’t create multiple
instances.
An IntentService automatically stops after the queue is completed. No
need to trigger stopService() or stopSelf().
In an IntentService, multiple intent calls are automatically Queued
and they would be executed sequentially.
An IntentService cannot run parallel operation like a Service.
Refer from Here
If someone can show me an example of something that you can be done with an IntentService and can not be done with a service and the other way around.
IntentService can not be used for Long Time Listening, Like for XMPP Listeners, its a single time operator, do the job and wave goodbye.
Also it has just one threadworker, but with a trick, you can use it as unlimited.
The Major Difference between a Service and an IntentService is described as follows:
Service :
1.A Service by default, runs on the application's main thread.(here no default worker thread is available).So the user needs to create a separate thread and do the required work in that thread.
2.Allows Multiple requests at a time.(Multi Threading)
IntentService :
1.Now, coming to IntentService, here a default worker thread is available to perform any operation. Note that - You need to implement onHandleIntent() method ,which receives the intent for each start request, where you can do the background work.
2.But it allows only one request at a time.
Hi in android I need send requests to a server every 30 seconds while the app is running without interfering with the user. Would it be advisable to do this in a service or thread, or a service with a new thread?
Cheers
Better to use Service.Use AlarmManager to call the service.In service write the code for send request to server
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
While thread is a concurrent unit of execution.You can use both for your purpose and another option is Runnable with Handler where you can call operation or code for every 30 seconds
Check out AlarmManager (as Chaitu said) as well as IntentService.
I suggest you use a Service rather than creating one thread. The Android developer site states the following:
Note that services, like other application objects, run in the main
thread of their hosting process. This means that, if your service is
going to do any CPU intensive (such as MP3 playback) or blocking (such
as networking) operations, it should spawn its own thread in which to
do that work. More information on this can be found in Processes and
Threads. The IntentService class is available as a standard
implementation of Service that has its own thread where it schedules
its work to be done.
Since the Service runs in the UI thread you should create a new thread in the service that makes request every 30 seconds to the server.
I hope this helps.
I will suggest you to write an IntentService not a simple Service and inside it write a static method to send a request to server with backoff time 30 seconds and in your activity just start the service.
To simply answer - "Should you use a service or a thread?"
A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.
If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service.
Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.