android-app-development-services-themexsoft

In our previous Tutorial, we discussed about Android UI Layouts . Here is the complete details about Android Services.

What are services ?

service is a component which runs in the background such as playing music, handle network transactions, interacting content providers etc..without direct interaction with the user and it works even if application is destroyed. A service can essentially take two states

  • Started
  • Bound

Started Services

A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. Normally, a started service performs a single operation and does not return a result to the caller. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.

Bound Services

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

 Visit : “Android Activity Life cycle”

Android Services Life cycle

A service has life cycle callback methods that you can implement to monitor changes in the service’s state and you can perform work at the appropriate stage.

service_lifecycle_rdc

 

As you can see in diagram there some method in  Unbounded service class for life cycle :

To create an service, you create a Java class that extends the Service base class.The Service base class defines various callback methods and the most important are given below

onCreate()

The system calls this method when the service is first created using onStartCommand() or onBind(). This call is required to perform one-time set-up

onStartCommand()

Android starts it and calls its onCreate method followed by the onStart method. If the service is already running, its onStart method is invoked again with the new intent(onStartCommand(Intent, int, int): service receives a call via startService.). So it’s quite possible and normal for a service’s onStart method to be called repeatedly in a single run of the service

onBind()

If a client needs a persistent connection to a service, it can call the Context.bindService method. This creates the service if it is not running, and calls onCreate but not onStart method. Instead, the onBind method is called with the client’s intent, and it returns an IBinder object that the client,can use to make further calls to the service. It’s quite normal for a service to have clients starting it and clients bound to it at the same time.

onUnbind()

This method is Called when the service will un-binded from activity when all clients have disconnected from a particular interface published by the service.

onDestroy()

when an activity calls stopService, or the service itself calls stopSelf, or the system decides to destroy the service.

onRebind

This method is called when you want to Re-bind service after calling un-bind method. when new clients have connected to the service, after it had early been notified that all had disconnected in its onUnbind(Intent)

 

To know more about the complete topics of Android,Click Here