Android Service

   A long running background process without any user interface is called Service. for example , consider a music player . The music may be started by an activity , but you want it to keep playing even when the user has moved on to a different program , so the code that does the actual playing should be in a service.

Types of Android Services :-

  1) Foreground Service

  2) Background Service 

  3) Bound Service



1) Foreground Service

       Foreground services are those services that are visible to the users. The users can interact with them at ease and track what’s happening. These services continue to run even when users are using other applications.

example :- Music Player

 2) Background Service 

         These services run in the background, such that the user can’t see or access them. These are the tasks that don’t need the user to know them.

example :- Syncing contacts or storing data

 3) Bound Service

           Bound service runs as long as some other application component is bound to it. Many components can bind to one service at a time, but once they all unbind, the service will destroy.


Steps to create Service :- 

  1) Create a class as a child of Service class.

  2) Service is abstract class contains one abstract method called onBind();

  3)  Some major methods in Service class are :- 

            onCreate()

            onStartCommand()

            onDestroy()

  4) When we start a Service if the service is not available in stack it will call onCreate() and  onStartCommand() methods.

  5) If the Service is already available then it will call onStartCommand() method.

  6) onDestroy() is called when we stop the Service.

  7) Service doesn't contain UI , we will manage Service from activity by using Intent.


  8) Same as Activity ever Service must be configure in Manifest.xml with the following tag inside <Application> tag.

     





Example of service:- 



Post a Comment

0 Comments