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

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.

We need to declare our service in Manifest. And since we will be downloading image from internet we need to take care of permissions as well. Add permission android.permission.INTERNET for internet, android.permission.READ_EXTERNAL_STORAGE and android.permission.WRITE_EXTERNAL_STORAGE for reading and writing to storage.

Update your AndroidManifest.xml as below

Let’s move over to MainActivity.java, We need to implement View.OnClickListener and override onClick(View) method to start IntentSevice from there.  After completion of task in IntentService, If required we need to communicate between Activity and IntentService. We can use simple BroadcastReceiver in our Activity class. We can register it while creating activity with specific IntentFilter, Intent braodcasted from IntentSevice after completion of task can be received in BroadcastReceiver.

In this article, we will use ResultReceiver for communication between Activity and IntentService. With IntentService start intent, we will add extras to intent for url and ResultReceiver. If ResultReceiver object passed to IntentService has Handler from Activity context, it can be used to sending result to Activity using send(int resultCode, Bundle resultData).

Modify your MainActivity.java as below

Create a class named SampleIntentService.java which will extend IntentService. Add constructor in order to specify name for backround thread and implement onHandleIntent(Intent). In onHandleIntent(Intent), we will retrieve ResultReceiver and url string from intent extras. Open HttpURLConnection for retreived url and write InputStream from connection to FileOutputStream directing to File on device. If task is completed or failed, we notify it via ResultReceiver object.

Your final code should look like as below.

You may also like...

Leave a Reply

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