Home > Tutorials > Working With Services In Android – IntentService

Working With Services In Android – IntentService

A service is an application component that can perform long-running operations in the background and it does not provide an user interface. For example, a service might handle network transactions, play music, perform file I/O, etc., all from the background.

Usually, there are two classes you can extend to create a service: you extend the Service class and override some callback methods, or extend the IntentService class and all you need to do is implement the onHandleIntent() method where you put all the code that should run in the background.

Extending the IntentService class is the simplest way to create a service. If you don’t need to handle multiple requests simultaneously, then this probably is the best option to choose.


In this post we will create a service using the IntentService class.

Well, our service won’t do much. This would be a proof-of-concept application, showing the creation process of a service. The mission of our service would be just to print a message in the LogCat, (pretty simple for a service :)), but in a real application you would write of course more advanced code, like playing music, or performing some I/O operations for example.

1. Create a new project in Eclipse:
Project: ServiceTest
Activity: ServiceTestActivity

2. Create a new class, say MyService, and do as follows:
– Extend the IntentService
– Provide a constructor with no arguments that calls the super constructor, with a name as an argument.
– Override the onHandleIntent(Intent intent); method.

public class MyService extends IntentService{

// No-arg constructor is required
public MyService() {

protected void onHandleIntent(Intent intent) {
   Log.d("TAG", "Service started");

   // The rest of the code that needs to run in the background


Note that it’s important that you provide a no-arg constructor, otherwise you may get an exception: InstantiationException


3. Register your service

Open the AndroidManifest file and add the following line as a child of <application> tag:

<application ......
   <service android:name=".MyService" />


4. Start the service

To start a service the startService(intent) method is used, which takes as an argument an intent with the specified service.

public void onCreate(Bundle savedInstanceState) {

   // Starting the service
   Intent intent =new Intent(this, MyService.class);
  1. praveshrohilla7
    April 1, 2012 at 7:13 pm

    can you tell how will you add “SyntaxHighlighter” plugin in your wordpress. because my filezilla is not connect with the wordpress server. and there is no plugin link on my admin page.

  2. akki
    January 17, 2013 at 5:54 am

    could u explain what’s the use of service in android

  3. Teo
    April 28, 2013 at 6:59 am

    Hello, I had a null pointer exception when I wrote in Manifest file:

    Instead of

    Just for info.

    • Teo
      April 28, 2013 at 7:02 am

      Sorry, I had a copy paste problem with the code. I wanted to say if I used “service” name tag instead of “intentservice” tag in Manifest file when MyClass extends IntentService I receive a null pointer exception.

  4. March 14, 2014 at 4:54 pm

    Hey! I’m at work browsing your blog from my new iphone!
    Just wanted to say I love reading your blog and look forward to all your posts!

    Carry on the excellent work!

  1. May 11, 2013 at 11:10 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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


Get every new post delivered to your Inbox.

Join 587 other followers

%d bloggers like this: