1 of 9

Service

Prasanna Kumar K R

2 of 9

Service

  • A Service is an application component that can perform long-running operations in the background.
  • It does not provide a user interface.
  • Once started, a service might continue running for some time, even after the user switches to another application.
  • Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC).

3 of 9

Foreground Service

  • A foreground service performs some operation that is noticeable to the user. (Example: audio app )
  • Foreground services must display a Notification.
  • Foreground services continue running even when the user isn't interacting with the app.

4 of 9

Background Service

  • A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

5 of 9

Bounded Service

  • Bound A service is bound when an application component binds to it by calling bindService().
  • A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC).
  • A bound service runs only as long as another application component is bound to it.
  • Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

6 of 9

7 of 9

Service Methods

  • onStartCommand()
    • startService() - requests that the service be started.
    • stop the service when its work is complete by calling stopSelf() or stopService().
  • onBind()
    • bindService() when another component wants to bind with the service

8 of 9

Service Methods (Cont…)

  • onCreate()
    • The system invokes this method to perform one-time setup procedures when the service is initially created (before it calls either onStartCommand() or onBind()).
    • If the service is already running, this method is not called.
  • onDestroy()
    • The system invokes this method when the service is no longer used and is being destroyed.
    • Your service should implement this to clean up any resources such as threads, registered listeners, or receivers.
    • This is the last call that the service receives.

9 of 9