Service LifeCycle

Here is the my updated tutorial of service n android. Please check those. Service in Android Part1 , Service in Android Part2 and Service in Android Part3.

A service is an application component that can run some long running task in the background without the need for a user interface. Some other application component can start the service and this service will then keep on running even if the user switches to another application.

A service can essentially take two forms:

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely (unbounded), even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

As you can see in diagram there some method in  Unbounded service class for life cycle :

startService(Intent Service)
This you must call to start un-bounded serviec

This method is Called when the service is first created

onStartCommand(Intent intent, int flags, int startId)
This method is called when service is started

onBind(Intent intent)
This method you must call if you want to bind with activity

onUnbind(Intent intent)
This method is Called when the service will un-binded from activity

onRebind(Intent intent)
This method is called when you want to Re-bind service after calling un-bind method

This method is called when The service is no longer used and is being destroyed

Let's create a small app to understand this..

App Name: ServiceLifeCycle
Package Name: com.sunil
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity

  1. package com.sunil;  
  3. import;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  11. public class MyActivity extends Activity   
  12.                     implements OnClickListener {  
  13.     private final static String TAG = "In this method: ";  
  14.     private Button startSerivce = null;  
  15.     private Button stopSerivce = null;  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  22.         startSerivce = (Button) findViewById(;  
  23.         startSerivce.setOnClickListener(this);  
  24.         stopSerivce = (Button) findViewById(;  
  25.         stopSerivce.setOnClickListener(this);  
  26.     }  
  28.     @Override  
  29.     public void onClick(View v) {  
  30.         if (startSerivce == v) {  
  31.             Log.i(TAG, "Activity starting service..");  
  32.             Intent serviceIntent = new Intent(this, MyService.class);  
  33.             startService(serviceIntent);  
  34.         } else {  
  35.             Intent in = new Intent(this, MyService.class);  
  36.             in.setAction("stop");  
  37.             stopService(in);  
  38.         }  
  39.     }  
  40. }  

  1. package com.sunil;
  2. import;  
  3. import android.content.Intent;  
  4. import android.os.IBinder;  
  5. import android.util.Log;  
  6. import android.widget.Toast;  
  8. public class MyService extends Service {  
  10.     private final static String TAG = "In this method: ";  
  11.     int mStartMode; // indicates how to behave if the service is killed  
  12.     IBinder mBinder; // interface for clients that bind  
  13.     boolean mAllowRebind; // indicates whether onRebind should be used  
  15.     @Override  
  16.     public void onCreate() {  
  17.         Log.i(TAG, "Service created");  
  18.         // The service is being created  
  19.     }  
  21.     @Override  
  22.     public int onStartCommand(Intent intent, int flags, int startId) {  
  23.         Log.i(TAG, "Service started");  
  25.         Toast.makeText(getBaseContext(), "Service has been started..",  
  26.                 Toast.LENGTH_SHORT).show();  
  27.         return mStartMode;  
  28.     }  
  30.     @Override  
  31.     public IBinder onBind(Intent intent) {  
  32.         // A client is binding to the service with bindService()  
  33.         Log.i(TAG, "Service binded");  
  34.         return mBinder;  
  35.     }  
  37.     @Override  
  38.     public boolean onUnbind(Intent intent) {  
  39.         // All clients have unbound with unbindService()  
  40.         Log.i(TAG, "Service un-binded");  
  41.         return mAllowRebind;  
  42.     }  
  44.     @Override  
  45.     public void onRebind(Intent intent) {  
  46.         // A client is binding to the service with Re-bindService(),  
  47.         // after onUnbind() has already been called  
  48.         Log.i(TAG, "Service re-binded");  
  49.     }  
  51.     @Override  
  52.     public void onDestroy() {  
  53.         // The service is no longer used and is being destroyed  
  54.         Log.i(TAG, "Service destroyed");  
  56.     }  
  58. }  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android=""  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:gravity="center">  
  8.     <LinearLayout  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_width="match_parent"  
  11.         android:id="@+id/linearLayout1"  
  12.         android:gravity="center">  
  13.         <Button  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_width="wrap_content"  
  16.             android:id="@+id/buttonStart"  
  17.             android:text="Start Service"></Button>  
  18.         <Button  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_width="wrap_content"  
  21.             android:id="@+id/buttonStop"  
  22.             android:text="Stop Serivce"></Button>  
  23.     </LinearLayout>  
  24. </LinearLayout>  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest  
  3.     xmlns:android=""  
  4.     package="com.sunil"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0">  
  7.     <uses-sdk android:minSdkVersion="8" />  
  9.     <application  
  10.         android:icon="@drawable/icon"  
  11.         android:label="@string/app_name">  
  12.         <activity  
  13.             android:name=".MyActivity"  
  14.             android:label="@string/app_name">  
  15.             <intent-filter>  
  16.             <action android:name="android.intent.action.MAIN" />  
  17.             <category android:name="android.intent.category.LAUNCHER" />  
  18.             </intent-filter>  
  19.         </activity>  
  21.         <service  
  22.             android:enabled="true"  
  23.             android:name=".MyService">  
  24.             <intent-filter>  
  25.             <action android:name="com.sunil.MyService">  
  26.             </action>  
  27.             </intent-filter>  
  28.         </service>  
  30.     </application>  
  31. </manifest>  

The output Screen will be like this..

You can download the complete source code zip file here : ServiceLifeCycle


I'd love to hear your thoughts!


  1. This comment has been removed by the author.

  2. Nice Tutorial!Great Job...if you are beginner then you can also visit

  3. Nice information about service cycle. The methods in the service life cycle are explained in an effective manner and my sincere thanks for sharing this post
    Android Training in Chennai

  4. Good and nice post, thanks for sharing your valuable information... very useful to me.. keep updating and rocks..

    Java Training in chennai | Java Training institute in chennai | Dot Net Training in chennai

  5. Thanks for your marvelous posting! It is very useful and good. Come on. I want to introduce the best aso services for you, I try it and I feel it is so good to rank app to top in app store search results, have you ever heard it?

  6. Nice post. I have been reading a lot of stuff on this topic in the last few months, but this article stands out with its simplicity & authenticity. Every passage made profound sense. Thanks a lot for this.
    MCA Project Center in Chennai | MCA Project Center in Velachery


Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me


Email *

Message *