In Android, service is a process which doesn’t need user interaction. It is a component which keep an app running in the background to perform long-running operations.

Types of Services

These are the three different types of services:

  • Foreground : A foreground service performs some operation that is noticeable to the user. When you use a foreground service, you must display a notification so that users are actively aware that the service is running. This notification cannot be dismissed unless the service is either stopped or removed from the foreground.
  • Background : A background service performs an operation that isn’t directly noticed by the user.
  • Bound : A service is bound when an application component binds to it by calling bindService(). 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.

Service Life Cycle

Life cycle of service will follow two different paths. A service is started when an application component, such as an activity calls startService() method. Once it started, it will run indefinitely in background even if the component that started is destroyed. To stop the started service call stopService() method or the service can stop itself by calling stopSelf() method.

A service is bound when another application component calls bindService() method. The bound service runs as long as another application component is bound to it. To unbind the service call unbindService() method.

Android Service Life Cycle

Create Service

To create a service, we need extends Service class or one of its existing subclasses. We must override some of the methods of the Service class. An activity can start the service by calling startService() which results in calling the service’s onStartCommand() method.

Following example create a service to play the ringtone.

public class MyService extends Service {

    private MediaPlayer player;
    public IBinder onBind(Intent intent) {
        return null;

    public int onStartCommand(Intent intent, int flags, int startId) {

        // Systems default ringtone
        player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);

        // Start the player
        // Service will be explicity started and stopped
        return START_STICKY;
    public void onDestroy() {

        // Stopping the player

The return value from onStartCommand() must be one of the following constants:

  • START_NOT_STICKY : If the system kills the service after onStartCommand() returns, do not recreate the service unless there are pending intents to deliver.
  • START_STICKY : If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent.
  • START_REDELIVER_INTENT : If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn.

Once we create a service, we need to register that in android manifest file (AndroidManifest.xml) using <service> element like as shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
        <!-- Defining the service class here --> 
        <service android:name=".MyService" />

Starting and Stopping Service

Component such as an activity, service or receiver can start the service using startService() method. In the following example, a service is started by activity.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonStart;
    private Button buttonStop;
    protected void onCreate(Bundle savedInstanceState) {

        buttonStart = (Button) findViewById(;
        buttonStop = (Button) findViewById(;
    public void onClick(View view) {

        if (view == buttonStart) {

            // Start service
            startService(new Intent(this, MyService.class));
        } else if (view == buttonStop) {

            // Stop service 
            stopService(new Intent(this, MyService.class));

Following is the layout file (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
            android:text="Start Service" />
            android:text="Stop Service" />


To start the service, tap on Start Service and ringtone will start ringing. And even if you close your application ringtone will keep ringing. To stop the service, tap on Stop Service button. As we can see in the above example, we can trigger the execution of a service via the startService(intent) method.

To stop the service, call stopService() method. A service can terminate itself by calling the stopSelf() method. This is typically done if the service finishes its work.