Android Programming – Android Service life cycle

We have discussed about Android Core Components and also walk through with an example to Create a Service (not an Android Service). Before we extend our Service to an Android Service; I would like to explain the Android Service life cycle through this Article. This will help you to understand how the Android Service will work and how to Activate the Service from the Client.

Services are one of the core components in Android. Either these can be run in a separate address space, or part of the Application’s address space. By default these will be in Application’s address space.

Android Applications use the Services, to perform some background tasks.

An Activity will start the Service to perform some tasks by calling startService or bindService methods.

Services can be Unbounded or Bounded. Unbounded services will be destroyed only when the Client stop the Service. Bounded services will be destroyed once all of its Clients are disconnected to the Service.

When the Service is activated with startService method; the Service will run until either stopService method is called or it’s stopSelf method is called. Whoever calling these methods (either the Activity, or the Service or other Applications), the Service will continuously run even though the component which has created the Service is stopped.

Unbounded Service

Once the Service is activated through startService method, below are the methods will be called during the Service life cycle.

onCreate – This will be called by the System when the Service is first created. The Application should NOT call this method directly. This is called by the System upon activating the Service using startService method.

onStartCommand  (in previous Android versions this one was called as onStart and deprecated in API level 5) – This method is called by the System whenever the client starts the Service by calling startService method.

onDestroy – System will call this method to remove the Service from the memory. This also should NOT be called by the Client directly.

Bounded Services

Another way of activating the Service is by calling bindService method. This way, there will be a bonding established between the Service and the Client. The Service will be destroyed once all of its clients are unbounded to the Service. Below are the methods called during the Service life cycle.

onCreate – This is the method  called by the System when the Service has been created by the Client by calling bindService method.

onBind – This method is called to bind the Client to the Service. The Service will return IBinder interface to the Client to bind to the Service; otherwise, it returns null if the Clients can not bind to the Service.

onUnbind – This method is called after all the Clients are disconnected to the Service.

onRebind – This is called when new Clients are connected to the Service.

onDestroy – As mentioned above, this is called before destroying the Service.

With this knowledge, we will convert already developed Service to an Android Service.

We discuss more on Android Programming in my upcoming Articles.

[..] David

One Comment

Add a Comment

Your email address will not be published.