Android Services |Android App Development With AndroidCource

Thursday 14 September 2017

Android Services

Introduction in Services


A service is a component which runs in the background without direct interaction with the user.

Service is a no user interface.

Service are used for long running operations. ex. Internet Downloads,check new Data etc..

A service can essentially take two states

1. Started

2. Bound


1.Started

A service is started when an application component, such as an activity, starts it by calling startService().

Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.

For example, it can download or upload a file over the network. When the operation is complete, the service should stop itself.


2. Bound

A service is bound when an application component binds to it by calling bindService().

A bound service runs only as long as another application component is bound to it.

Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC).


Services Lifecycle



Life cycle of services show when the service is created with startService() and the diagram on the right shows the life cycle when the service is created with bindService().


Lifecycle Callback Methods

onStartCommand()

The system invokes this method by calling startService() when another component (such as an activity) requests that the service be started.

When this method executes, the service is started and can run in the background indefinitely.

onBind()

The system invokes this method by calling bindService() when another component wants to bind with the service (such as to perform RPC).

In your implementation of this method, you must provide an interface that clients use to communicate with the service by returning an IBinder.

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.

onUnbind()

This method when all clients have disconnected from a particular interface published by the service.

onRebind()

This method when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent).

onDestroy()

When the service is no longer used and is being destroyed.


Service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.





0 comments:

Post a Comment