Friday, 21 September 2012

Service


Service

A service is an application component that can run some long running task in the background without the need for a user interface. Some other application component can start the service and this service will then keep on running even if the user switches to another application.

There are two types of service components:

1.     Started Service

2.      Bounded Service

Started Service: 

                                 This service is started by another application component. Once started it keeps running until someone stops the service. This type of service provides several callbacks which is the topic of this tutorial.

Bounded Service:

                               A service is bound when another application component invokes the bindService method. Service binding is used to perform client-server like communication between the service and the caller. These invocations can be performed between different processes as well. Multiple callers can bind to a service. The service stays active until there is atleast one caller bound to the service.

Life Cycle of Started Service

When You are launching any service for first time , on that time it will call onCreate(). If the service is already launched , then from the second time onStartCommand() will call, it won't call onCreate().For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. Once the onDestroy() will called, the service will terminate. 

Life Cycle of Bounded Service


Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the service's IBinder).

No comments:

Post a Comment