Services

Framework to move the execution of long running tasks to the background.Services run as long as they are needed and survive the destruction of the application process.

Categories:

  • Foreground: noticeable to the end user. Must display a status bar. Continue running when there is no interaction with the application.
  • Background: not noticeable by the end user.
  • Bound: the service that allows Android components to bind to it. To perform binding, we must call the bindService() method.

Basics

To define a service, extend the Service class.

  • onStartCommand(): This method is executed when the startService() method is triggered by some Android component. After the method is executed, Android service is started and can run in the background indefinitely. To stop this service, you must execute the stopService() method that has an opposite functionality to the startService() method.
  • onBind(): To bind to the service from another Android component, use the bindService() method. After we bind, the onBind() method is executed. In your service implementation of this method, you must provide an interface that clients use to communicate with the service by returning an Ibinder class instance. Implementing this method is not optional, but if you don't plan to bind to the service, just return null.
  • onCreate(): This method is executed when the service is created. It is not executed if the service is already running.
  • onDestroy(): This method is executed when the service is destroyed. Override this method and perform all cleanup tasks for your service here.
  • onUnbind(): This method is executed when we unbind from the service.

results matching ""

    No results matching ""