Started Service in Android

Here in this post I would like to discuss about Started Service.

First of all what we need to do is create a Service.

How is it done?

There are basically two ways for that. First one is to extend the class Service which is the base class for all Services. Second option is extending IntentService which is the subclass of the Service class.

It’s wise that we create a subclass of the class Service when there are a multiple requests for our service to start simultaneously.

IntentService is used when our service needs to handle all start requests, one at a time and not simultaneously.  So here, I’m extending  IntentService  in my program as I’m just showing a Toast and would display a message at the LogCat.

So here we go 🙂

Let’s see the XML code for the layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="@string/hello" />
        android:text="@string/textbtn" />

Here, I’ve placed a button in MyStartedServiceActivity which is my activity, clicking on which would start the service.

Let’s see the code:

package com.deepthi.mystartedservice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyStartedServiceActivity extends Activity {
    /** Called when the activity is first created. */
      Button start;
    public void onCreate(Bundle savedInstanceState) {
        start.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(getApplicationContext(), myservice.class);

Just look at the above code. Here the service is started by passing an Intent which specifies the Service to be started, to startService().  The system then calls the onStartCommand() and passes the intent to it.

Now, let’s look at the code of myservice :

package com.deepthi.mystartedservice;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class myservice extends IntentService{
      public myservice() {
            // TODO Auto-generated constructor stub
      protected void onHandleIntent(Intent intent) {
            // TODO Auto-generated method stub
            Log.w("TAG","Started my Service");
      public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Starting my Service", Toast.LENGTH_LONG).show();
            return super.onStartCommand(intent, flags, startId);

Here, we just need a constructor and then we need to override and implement onHandleIntent() .

Note: Don’t forget to register service at the Manifest File.

Here is the Manifest File:

        android:label="@string/app_name" >
            android:name=".MyStartedServiceActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

The Screen Shots of the Out Put are shown below:

Starting Service by clicking the button.

The LogCat :

Hope this post was useful. 🙂


, , , , , , ,

  1. #1 by venky on May 9, 2012 - 6:08 pm

    post new things mam

    • #2 by Deepthi G on May 13, 2012 - 3:48 pm

      Hi Venky,

      There were network problems at my home and therefore was unable to access the internet..I’ll post new articles soon for sure. Thanks a lot for reading.. Keep reading..:)

  2. #3 by nehalokhande1 on April 22, 2013 - 2:49 pm

    your articles are really very useful pls post more articles and u dont seem to be a beginner in android…………nways well done and keep it up!

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

%d bloggers like this: