IntentService: Working and Advantages of using them

IntentService handles asynchronous requests sent as Intents on demand basis. Request is made via startService(Intent) call. Service is started if needed and it will handled all the requests in turn using a background thread. It will stop by itself as soon as it is done performing the task.


[su_button url=”https://github.com/androidsrc/SampleIntentService” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Complete Source Code[/su_b[/su_button]p style="text-align: justify;">

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

IntentService works on “work queue processor” mechanism and help to take load off from your application main 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.

IntentService should be used if we don’t want our service to handle multiple requests simultaneously. For using IntentService, all we need to do is implement onHandleIntent(Intent) method to perform the requested work and provide a constructor for service which must call super("WorkerThreadName").

Sample IntentService Application

We will create an application using IntentService which will download image from internet in background thread of IntentService. We will have main activity with one EditText to specify  url of image and one button to start IntentService from onClick() event.

Lets start with res/layout/activity_main.xmlModify your layout file as below.

You may also like...

1 Response

  1. February 18, 2015

    […] We can use IntentService in conjunction with ResultReceiver also for acheiving same result. If interested, you can check our detailed tutorial. Android IntentService: Working and Advantages […]

Leave a Reply

Your email address will not be published. Required fields are marked *