MY mENU


Monday, 13 February 2012

Services in Android

Services :- A service is a component that runs in the background for an indefinite period of time, and which doesn’t provide a user interface. As with an activity, a service runs on the
process’s main thread; it must spawn another thread to perform a time-consuming operation. Services support true multitasking for Android, as they can run in their own processWe can start the another services and it will continue to run in the background even if the user switches to another application. It even perform inter process communication (IPC).

A service is not a separate process, although it can be specified to run in a separate process. Also, a service is not a thread. Instead, a service lets the app tell Android about something it wants to be doing in the background (even when the user is not directly interacting with the app), and lets the app expose some of its functionality to other apps.

This service is used handle network transactions, play music, perform file I/O, or interact with a content provider, all running in the background.


started and bundle are two form of service.

1.Started :- The service start when We called the start service method by the activity. when another component calls startService() then services is start. Once service started,  a service can run in the background indefinitely and must stop itself by calling stopSelf(), even if the component that started it is destroyed. 

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

2.Bound :- This method is used to bound the service when an application component binds to it by calling bindService(). This 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 inter-process communication (IPC).The client can close the connection by calling unbindService(). Multiple clients can bind to the same service and when all of them unbind, the system destroys the service. 


Service needs to be declared in the AndroidManifest.xml via a service android:name="yourclasss" and the implementing class must extend the class Service or one of its sub-classes.
You can also specify that your Service should run in a different process then your application via the android:process=":process_description" attribute. This way the service gets its own process and has its own memory. Therefore any low running operation in the Service, e.g. a garbage collection, will not affect the user interface.

Service will not automatically run in its own thread. Without the process attribute they run the main thread of their hosting process. Therefore you should run performance intensive tasks in the background.

Life Cycle of Service : Life cycle of service is same as Activity life cycle. Service has many life-cycle callback methods. There are some life cycle method :

1.startService() :-This method is called when application service be started.This method return  the ComponentName of the actual service.  

public abstract ComponentName startService (Intent service)

2. onCreate() :- This method is called  by the system when the service is first created. Do not call this method directly.
              
                                public void onCreate ()

3.onStartCommand() :- This method is called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. 
       
                          public int onStartCommand (Intent intent, int flags, int startId)

4.onBind() :- This method is return the communication channel to the service. 
This method return null  if clients can not bind to the service otherwise return  IBinder

                           public abstract IBinder onBind (Intent intent)

5.onUnbind() :- This method is called when all clients have disconnected from a particular interface published by the service.This method return boolean value.The default implementation does nothing and returns false.

                            public boolean onUnbind (Intent intent)

6.onRebind() :-   This method is called when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent).
                             public void onRebind (Intent intent)

7.onDestroy() :-This method is called by the system to notify a Service that it is no longer used and is being removed

                            public void onDestroy ()

Services are classified as local or remote.
  • A local service runs in the same process as the rest of the app. Such services make it easy to implement background tasks.
  • A remote service runs in a separate process. Such services let you perform interprocess communications.

No comments:

Post a Comment