ServiceCompat

Added in 1.1.0

public final class ServiceCompat


Helper for accessing features in android.app.Service.

Summary

Constants

static final 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.

static final int

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

static final int

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

Public methods

static void
startForeground(
    @NonNull Service service,
    int id,
    @NonNull Notification notification,
    int foregroundServiceType
)

startForeground with the third parameter foregroundServiceType was added in Q.

static void
stopForeground(@NonNull Service service, int flags)

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
public static final int START_STICKY = 1

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
public static final int STOP_FOREGROUND_DETACH = 2

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
public static final int STOP_FOREGROUND_REMOVE = 1

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 methods

startForeground

Added in 1.12.0
public static void startForeground(
    @NonNull Service service,
    int id,
    @NonNull Notification notification,
    int foregroundServiceType
)

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
public static void stopForeground(@NonNull Service service, int flags)

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

Parameters
@NonNull Service service

service to remove.

int flags

Additional behavior options: STOP_FOREGROUND_REMOVE, STOP_FOREGROUND_DETACH.

See also
startForeground