Droid Services
Droid Services
1 Foreground: Performs some operation that is noticeable to the user. For example, an audio app would use a foreground service
to play an audio track.
Foreground services must display a Notification. Foreground services continue running even when the user isn't interacting
with the app.
When you use a foreground service, you must display a notification so that users are actively aware that the service is running.
This notification cannot be dismissed unless the service is either stopped or removed from the foreground.
2 Background: 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.
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.
Service callbacks
• To create a service, subclass of Service or use one of its existing subclasses.
• In your implementation, you must override some callback methods that handle key aspects of the service lifecycle
and provide a mechanism that allows the components to bind to the service, if appropriate.
• Bellow are the most important callback methods that you should override:
1 onStartCommand() The system invokes this method by calling startService() when another component (such as an activity)
requests that the service be started.
When this method executes, the service is started and can run in the background indefinitely. If you implement this, it is your
responsibility to stop the service when its work is complete by calling stopSelf() or stopService().
NB: If you only want to provide binding, you don't need to implement this method.
2 onBind() The system invokes this method by calling bindService() when another component wants to bind with the service
(such as to perform RPC).
In your implementation of this method, you must provide an interface that clients use to communicate with the service by
returning an IBinder. You must always implement this method; however, if you don't want to allow binding, you should return
null..
Cont …
3 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()).
4 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.
https://developer.android.com/develop/background-work/services
https://developer.android.com/guide/components/foreground-services
https://developer.android.com/topic/libraries/architecture/workmanager