Android Service Life Cycle
Android Tutorials

Android Service Life Cycle Tutorial

Android Service

Android Service is an application component that is used to perform long-running operations such a playing music in the background, handle network transactions etc all from the background. Android Service does not provide a user interface.

Android Service runs indefinitely in the background unless they explicitly stopped or destroyed.

Android application component can start a service, and the service continues to run in the background indefinitely, even if the component that started the service is destroyed. That is the service continues to run in the background even if the user switches to another application.

Additionally, an android application component can bind to a service for performing interprocess communication (IPC). As discussed service can running in the background even if the application is killed unless it stops itself by calling stopself() or is stopped by a Android application component by calling stopService().

As explained above service can either be started or bound. A service can basically exist in two states as shown below:

Started

A service is said to be started only when an android application component, such as an activity, starts the service by calling the method startService().

Bound

A service is said to be bound when an android application component binds to service by calling the method bindService(). A bound service offers a client-server interface that allows android application components to interact with the service, send requests, receive results.

Android Service Life Cycle

Android service life cycle have life cycle methods same as Activity life cycle methods.

Android Service Life Cycle
                              Service Life Cycle Method

onStartCommand()

The system invokes this method by calling startService() method when another component (such as an activity) requests that the service be started. When the onStartCommand() method executes, the service is started and can run in the background indefinitely. If you implement the service using the above method, it is your responsibility to stop the service when its work is complete by calling either stopSelf() or stopService().

onBind()

When another component wants to bind with the service (such as to perform RPC)  the system invokes this method by calling bindService() . In this method implementation, you must provide an interface that clients use to communicate with the service that we already started by returning an Ibinder. If you don’t want to bind the service with an android application component the onBind() method must return null.

onUnbind()    

The android system calls method when all components (activity) have disconnected from a specific interface published by the service.

onRebind()       

The android system calls this method when an application component have linked to the service, after it had disconnected from the service by calling the method Unbind().

onCreate()

The android system calls this method when the service is initially created using onStartCommand() or onBind().

onDestroy()

The system calls this method when the service is no longer being used.  You can carry out this to clean up any resources like threads, registered listeners, receivers, and so on allotted in the onCreate() method at the time of service creation.

One Reply to “Android Service Life Cycle Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *