Android

adplus-dvertising
Type of Android Component: Service
Previous Home Next

his application component operations is running in the background. This component does not provide a user interface. We 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 interprocess communication (IPC).

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.
  2. For example, A good example, download or upload a file over the network. When the operation is done, the service should stop itself.
  3. 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 interprocess 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.

Life Cycle of Service

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

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

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

  5. onStartCommand() : Thsi meyhod 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.
  6. public int onStartCommand (Intent intent, int flags, int startId)

  7. 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
  8. public abstract IBinder onBind (Intent intent)

  9. 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.
  10. public boolean onUnbind (Intent intent)

  11. 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).
  12. public void onRebind (Intent intent)

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

Previous Home Next