Thursday, 27 August 2015

Android Programming Tutorials 19 : Services In Android

Services:


A service is a component that runs in the background to perform long-running operations without needing to interact with the user. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. A service can essentially take two states.
                                              OR 
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.

A service can essentially take two forms:

Unbounded:
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 (unbounded), even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.


Bound:
A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). 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.

Android Service Lifecycle:

The lifecycle for a service is similar to that for an activity, but different in a few important details:



startService(Intent Service):
This you must call to start un-bounded serviec

onCreate()
This method is Called when the service is first created

onStartCommand(Intent intent, int flags, int startId)
This method is called when service is started

onBind(Intent intent)
This method you must call if you want to bind with activity

onUnbind(Intent intent)
This method is Called when the service will un-binded from activity

onRebind(Intent intent)
This method is called when you want to Re-bind service after calling un-bind method

onDestroy()

This method is called when The service is no longer used and is being destroyed. 

Two modes of operation depending on the value returned by the onStartCommand method:
– constant START_STICKY is used for services that are explicitly started and stopped as needed.
– constants START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.
That’s all for now. I have also attached the life-cycle describing diagrams from Android SDK documentation.





Please Send Your Comments To ambilpura.sunil@gmail.com

Stay Tune For Next Tutorial... Services Example In Android:

No comments:

Post a Comment