Intent Services: An Android lesser Known Topic

Hello all android geeks there!! So this blog basically talks about one of the lesser known topics of android that is Intent Services. First of all let me talk in the language that you all guys easily understand and that is in pure technical terms.

Technical specifications:

Intent Services is an abstract class which extends service class.
It has got one public constructor public Intent Service(String name).
Got 7 public methods
onBind(Intent intent) returns IBinder object
onCreate() returns void
onDestroy() returns void
onStart(Intent intent, int startId) returns void
onStartCommand(Intent intent, int flags, int startId)
setIntentRedelivery(boolean enabled)
and 1 protected method
onHandleIntent(Intent intent)

Now I think that was more than enough of technical specs for you all. Let’s start with some functional and useful aspects of Intent Services.

Intent Service is one of the simplest ways to do some long term background work or any network related task or any kind of processing off the UI thread of your application .Off course after reading this much of line the thing that must have popped out of your mind is AsyncTask.

Am I right? I think I am…

Well guys using AsyncTask class is a conventional way of doing things. Go get yourself a cup of coffee if you are feeling bit snooze.. The AsyncTask will always take over the main thread at least twice (with its pre- and post-execute methods), and it must be owned by an activity that is able to draw to the screen and simply there’s no absolute need to launch an AsyncTask and manage it each and every time you have more processing.

Instead, you simply define your own service, package up an intent with the appropriate data you want to send for processing, and start the service. One good reason to use an Intent Service is when you have work that needs to occur off the main thread to keep the application responsive and efficient. Another reason is when you may have multiple processing requests, and they need to be queued up and handled on the fly.

It is worth noting here that if there are multiple intents that are received, they all will NOT execute in parallel. The Intent Service class spawns a SINGLE worker thread. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially. And coders the awesome part is yet to come, you don’t have to manage the lifecycle of the spawned thread.

At this point of time I remember one saying “Just Use And Throw”, So in our context Intent Services is “Start Service And Forget It” yes you got it right you don’t even have to bother about stopping the service, when each intent is delivered and being serviced the android platform automatically stops the service by itself. Isn’t that awesome……..

So guys if the above said is more than you can digest theoretically then don’t worry you don’t have to get some acidity reliever, I am coming to the part which you love the most.

Implementation details:
Extend your class (say MyIntentService) as follows:

class MyIntentService extends IntentService {
public MyIntentService() {
super ("com.mindfire.MyIntentService");
protected void onHandleIntent(Intent intent) {
// call to any long running task !

Thats it. You are all set ! Now, you can trigger this by calling the startService(Intent) method. When for the first time this service is started by any Intent then only the onCreate() method of this service would be called and after that any call to startService() would result to an implicit call to onHandleIntent() method.
So guys and gals here is a summary of all said by me

The Intent Service does the following:

•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.
Cheers!! You have just added a new weapon in your android armoury….

Byee till my next post……Be happy and keep others happy

Written By: Jitendra Singh  , Android Developer, Mindfire Solutions


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s