ServiceCompat

Added in 1.1.0

class ServiceCompat


Helper for accessing features in android.app.Service.

Summary

Constants

const Int

Constant to return from onStartCommand: if this service's process is killed while it is started (after returning from onStartCommand), then leave it in the started state but don't retain this delivered intent.

const Int

Flag for stopForeground: if set, the notification previously provided to startForeground will be detached from the service.

const Int

Flag for stopForeground: if set, the notification previously provided to startForeground will be removed.

Public functions

java-static Unit
startForeground(
    service: Service,
    id: Int,
    notification: Notification,
    foregroundServiceType: Int
)

startForeground with the third parameter foregroundServiceType was added in Q.

java-static Unit
stopForeground(service: Service, flags: Int)

Remove the passed service from foreground state, allowing it to be killed if more memory is needed.

Constants

START_STICKY

Added in 1.1.0
const val START_STICKY = 1: Int

Constant to return from onStartCommand: if this service's process is killed while it is started (after returning from onStartCommand), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand after creating the new service instance; if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this.

This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

STOP_FOREGROUND_DETACH

Added in 1.1.0
const val STOP_FOREGROUND_DETACH = 2: Int

Flag for stopForeground: if set, the notification previously provided to startForeground will be detached from the service. Only makes sense when STOP_FOREGROUND_REMOVE is not set -- in this case, the notification will remain shown, but be completely detached from the service and so no longer changed except through direct calls to the notification manager.

This flag will only work on N and later. It doesn't have any effect on earlier platform versions.

STOP_FOREGROUND_REMOVE

Added in 1.1.0
const val STOP_FOREGROUND_REMOVE = 1: Int

Flag for stopForeground: if set, the notification previously provided to startForeground will be removed. Otherwise it will remain until a later call (to startForeground or stopForeground removes it, or the service is destroyed.

Public functions

startForeground

Added in 1.12.0
java-static fun startForeground(
    service: Service,
    id: Int,
    notification: Notification,
    foregroundServiceType: Int
): Unit

startForeground with the third parameter foregroundServiceType was added in Q.

Before SDK Version Q, this method call should call startForeground without the foregroundServiceType parameter.

Beginning with SDK Version Q, the allowed foregroundServiceType are:

Beginning with SDK Version UPSIDE_DOWN_CAKE, apps targeting SDK Version UPSIDE_DOWN_CAKE is not allowed to use FOREGROUND_SERVICE_TYPE_NONE. The allowed foregroundServiceType are:

stopForeground

Added in 1.1.0
java-static fun stopForeground(service: Service, flags: Int): Unit

Remove the passed service from foreground state, allowing it to be killed if more memory is needed.

Parameters
service: Service

service to remove.

flags: Int

Additional behavior options: STOP_FOREGROUND_REMOVE, STOP_FOREGROUND_DETACH.

See also
startForeground