Open top menu

In this tutorial we will explain how to implement Android service lifecycle.

Service: is an long-runing application component in background and dose not provide user-interface.
the service other application component service can be start automatically after OnStart service used this method onStartCommand().

Service make a call two way

  1. saterted;-startService()
  2. bound:-BindService()

The Basice android service lifecycle-
  • onStartCommand()-
Request that the service be started, by call starService().via this method,executed the service started in application background.
  • onBind()-
this method when another component to bind with the service by calling bindService(). provide an interface that client used to communicate with the service by an bBinder .
  • onCreate()-
this method  when the service is first created to proceed one time. before it call onStartCommend() or onBind() and this method already run in background.
  • onDestroy()- 

 This method when the service is no longer used and is being destroyed then your service is clean any resource.

package com.example.androidbeginnerpoint;

import android.content.Intent;
import android.os.IBinder;

public class MainActivity extends Service {

    int bStartMode;       // indicates how to behave if the service is killed
    IBinder bBinder;      // interface for clients that bind
    boolean bAllowRebind; // indicates whether onRebind should be used

    public void onCreate() {
        // The service is being created
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService
        return bStartMode;
    public IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService
        return bBinder;
    public boolean onUnbind(Intent intent) {
        // All clients have unbound with unbindService
        return bAllowRebind;
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService
        // after onUnbind has already been called
    public void onDestroy() {
        // The service is no longer used and is being destroyed