ServiceCompat
class ServiceCompat
Helper for accessing features in android.app.Service.
Summary
Constants |
|
|---|---|
const Int |
START_STICKY = 1Constant to return from |
const Int |
Flag for |
const Int |
Flag for |
Public functions |
|
|---|---|
java-static Unit |
startForeground(
|
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
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
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
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
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:
FOREGROUND_SERVICE_TYPE_MANIFESTFOREGROUND_SERVICE_TYPE_NONEFOREGROUND_SERVICE_TYPE_DATA_SYNCFOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACKFOREGROUND_SERVICE_TYPE_PHONE_CALLFOREGROUND_SERVICE_TYPE_LOCATIONFOREGROUND_SERVICE_TYPE_CONNECTED_DEVICEFOREGROUND_SERVICE_TYPE_MEDIA_PROJECTIONFOREGROUND_SERVICE_TYPE_CAMERAFOREGROUND_SERVICE_TYPE_MICROPHONE
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:
FOREGROUND_SERVICE_TYPE_MANIFESTFOREGROUND_SERVICE_TYPE_DATA_SYNCFOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACKFOREGROUND_SERVICE_TYPE_PHONE_CALLFOREGROUND_SERVICE_TYPE_LOCATIONFOREGROUND_SERVICE_TYPE_CONNECTED_DEVICEFOREGROUND_SERVICE_TYPE_MEDIA_PROJECTIONFOREGROUND_SERVICE_TYPE_CAMERAFOREGROUND_SERVICE_TYPE_MICROPHONEFOREGROUND_SERVICE_TYPE_HEALTHFOREGROUND_SERVICE_TYPE_REMOTE_MESSAGINGFOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTEDFOREGROUND_SERVICE_TYPE_SHORT_SERVICEFOREGROUND_SERVICE_TYPE_SPECIAL_USE
| See also | |
|---|---|
startForeground |
|
startForeground |
stopForeground
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: |
| See also | |
|---|---|
startForeground |