Android Service

Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn’t has any UI (user interface).

  1. A service is a application component used to perform long running tasks in background.
  2. A service doesn’t have any user interface and neither can directly communicate to an activity.
  3. A service can run in the background indefinitely, even if component that started the service is destroyed.
  4. Usually a service always performs a single operation and stops itself once intended task is complete.
  5. A service runs in the main thread of the application instance. It doesn’t create its own thread. If your service is going to do any long running blocking operation, it might cause Application Not Responding (ANR).  And hence, you should create a new thread within the service.


Android Service lifecycle

There can be two forms of a service.The lifecycle of service can follow two different paths: started or bound.

  1. Started
  2. Bound

       1) Started Service

A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.

       2) Bound Service

A service is bound when another component (e.g. client) calls bindService() method. The client can unbind the service by calling the unbindService() method.

The service cannot be stopped until all clients unbind the service.



This method is called when the service be started, by calling startService(). Once this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your responsibility to stop the service when its work is done, by callingstopSelf() or stopService().  If you are defining your service as, bounded service then you don’t need to implement this method.


You need to override this method, only if you are defining your service as bounded service. This method is called, when another component wants to bind with the service by callingbindService(). In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder. You must always implement this method, but if you don’t want to allow binding, then you should return null.


This method is called while the service is first created. Here all the service initialization is done. This method is never called again.


The system calls this method when the service is no longer used and is being destroyed. This method is used to, clean up any resources such as threads, registered listeners, receivers, etc. This is the last call the service receives.


How to Create a Service

Create a new class and extend it from You need to override onStartCommand(),onBind(), onCreate() and onDestroy() method to handle the service lifecycle.

Declaring Service in the Manifest

In theory, A service can be called from other application unless it is restricted.  You can ensure that your service is available to only your app by including the android:exported attribute and setting it to “false”. This effectively stops other apps from starting your service, even when using an explicit intent.

Starting a Service

You can start a service from an activity or other application component by passing an Intent tostartService(). The Android system calls the service’s onStartCommand() method and passes it the Intent.

In our example, we will start service by calling startService() method while start service button is clicked.