3.6 Android Services

Android Service is already introduced to you in previous chapters. Service is an Android application component which doesn’t provide the user interface. They perform long running operations in the background. Android Service can take two forms: Started and Bound. We already know these things and now it is high time we knew a little more about the more concepts.  

3.6.1 Android Forms of Service

Service is initiated by android application components and thus service can take any of the two forms as shown in the figure below. They can also be combined. They can be started and then bonded and in that case we have to implement proper couple callback methods, i.e., components need to call onStartCommand() method to start service and then onBind() method to initiate binding. 

          Android Forms of Service

                                                    Android Service can take two forms

Let us study them separately, one-by one so that things are clearer to you:

  • Service is started: Android Application components start service by calling the method startService(). If a service is started it can run in the background forever. For example a downloader which downloads whenever it receives the command of downloading. After downloading it terminates itself. Generally they target a single operation. It will run in the background until destroyed explicitly or do it thyself. Diagram explains this procedure.

          Service started by Android application component

                                       example of service started by Android application component


  • Service is bound:  Android Application component bind service by calling bindService() method. It offers a client-server interface through which the two applications in bond interact with each other. The lifetime of this service form is not long. It runs as long as the components are bonded together. In case, there are multiple components bonded together, then service will run as long as the last component is bonded.

          Android Bind Service Example

                                                  Android Bind Service Example

Components of different applications can be a part of your service. To implement privacy it has to be explicitly declared as private in manifest file. This is just like blocking unwanted people on facebook. It’s fun, trust me.

Now it is recommended to create a separate thread, if you want your application to do something only when user interaction is required by the application like your music player. It is so as a service always runs on the main application thread which may raise the Application Not Responding (ANR) errors very often, which in turn, disturbs the working of other activities.

  • 3.6.2 Life Cycle of service

    Like Android activities, Android services do have a life cycle, but the life cycle strategy is different. Services are started, stopped and controlled by other application components which include activities, broadcast receivers or even other services. We have studied the priority hierarchy where we have studied that running services have higher priority than inactive or stopped activities. Only in case of short of resources, android system may terminate the running service.  In that case as soon as resources are available, services are restarted automatically.

    Managing the life cycle of a service is very important, especially its creation and termination as they can run in background invisibly without notifying user. We have two forms of services. In between onCreate() method and onDestroy() method we have two different paths. Complete life cycle of service is between the time when onCreate() is called to the time onDestroy() returns the control. All the initial set up is done at time when onCreate() is called. Resources are released after the call to onDestroy(). Unbound or bound these callback methods are common to all services. Now let us go through the different life cycle paths.   

  • Life cycle of Android Started Service

  • This type of service is created after a call to startService() by any other application component. In this case service must kill itself by calling stopSelf() method. If you want another component to kill the service then, stopService() method should be called. System destroys service after it terminates

          Android Started Service Lifecycle

                                              Figure Lifecycle of Android Started Service

  • Life cycle of Android Bound Service

This service is created when a client component calls the bindService() method. There is an interface called IBinder through which client communicates with the service. The communication connection is terminated as soon as unbindService() is called. Multiple clients can bind to the same service. System destroys service as soon as all member components unbind. Unlike started service, stopSelf() method is not required. Let us see the life cycle path of a bound service.

          Lifecycle of Android Bound Service

                                 Figure Android Bound Service Lifecycle

From both the diagrams it is crystal clear that two service types have different life cycle strategies. Now active life time of unbound service is the entire life cycle of service even after onStartCommand() returns whereas bound service’s active lifetime ends as soon as onUnbind() returns. It is very important to remember that an unbound service can be bonded anytime.