Android Intent Service

What is Intent Service:

Intent Service is a base class for service. This handles asynchronous requests 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 It will stop by itself as soon as it is done performing the task.

All requests are handled on a single worker thread.

How to create Intent Service:

For using IntentService, create a class which extends IntentService and implement onHandleIntent(Intent). It will receive requests and handle them in background thread.

A single background thread is used to handle all the requests and requests are processed one by one. It might take time to process one request and other one has to wait for that time. When all requests have been handled, the IntentService stops itself.

public class MyService extends IntentService {
    MyService () 
    protected void onHandleIntent(Intent intent) {
     //This method is invoked on the worker thread with a request to process 

  • At a time it will process only one request
  • Can not be interrupted
  • 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.the request waits until the first operation is finished.
  • It can't interact directly with your user interface.

Differences between Service & IntentService:
  • Service uses application main thread. Intent Service create worker thread to perform operations.
  • 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. 
  • The Service is triggered calling to method onStartService(). The IntentService is triggered using an Intent.
  • The Service runs in background but it runs on the Main Thread of the application. The IntentService runs on a separate worker thread.

No comments:

Post a Comment