What is Service?

Android Service 

Services in Android are a special component that facilitates an application to run in the background in order to perform long-running operation tasks.The prime aim of a service is to ensure that the application remains active in the background so that the user can operate multiple applications at the same time.A service can run continuously in the background even if the application is closed or the user switches to another application. 



1. Foreground Services:

Services that notify the user about its ongoing operations are termed as Foreground Services. Users can interact with the service by the notifications provided about the ongoing task. Such as in downloading a file, the user can keep track of the progress in downloading and can also pause and resume the process.

2. Background Services:

Background services do not require any user intervention. These services do not notify the user about ongoing background tasks and users also cannot access them. The process like schedule syncing of data or storing of data fall under this service.

3. Bound Services:

This type of android service allows the components of the application like activity to bound themselves with it. Bound services perform their task as long as any application component is bound to it. More than one component is allowed to bind themselves with a service at a time. In order to bind an application component with a service bindService() method is used. 

Example: boundService

The Life Cycle of Android Services

In android, services have 2 possible paths to complete its life cycle namely UnBounded and Bounded.

1. Unbounded Service:

By following this path, a service will initiate when an application component calls the startService() method. Once initiated, the service can run continuously in the background even if the component is destroyed which was responsible for the start of the service.

Two option are available to stop the execution of service:
  • By calling stopService() method,
  • The service can stop itself by using stopSelf() method.

2. Bounded Service:

It can be treated as a server in a client-server interface. By following this path, android application components can send requests to the service and can fetch results. A service is termed as bounded when an application component binds itself with a service by calling bindService() method. To stop the execution of this service, all the components must unbind themselves from the service by using unbindService() method.


Comments