# ServiceCompat

# ServiceCompat

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/app/ServiceCompat.java+class:androidx.core.app.ServiceCompat)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/app/ServiceCompat "View this page in Java")

<br />

```
class ServiceCompat
```

<br />

*** ** * ** ***

Helper for accessing features in[android.app.Service](https://developer.android.com/reference/android/app/Service.html).

## Summary

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [START_STICKY](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#START_STICKY())` = 1` Constant to return from[onStartCommand](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)): if this service's process is killed while it is started (after returning from[onStartCommand](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))), then leave it in the started state but don't retain this delivered intent. |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [STOP_FOREGROUND_DETACH](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#STOP_FOREGROUND_DETACH())` = 2` Flag for[stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)): if set, the notification previously provided to[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))will be detached from the service.                                                                 |
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [STOP_FOREGROUND_REMOVE](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#STOP_FOREGROUND_REMOVE())` = 1` Flag for[stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)): if set, the notification previously provided to[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))will be removed.                                                                                   |

|                                    ### Public functions                                     |
|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [startForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#startForeground(android.app.Service,int,android.app.Notification,int))`(` ` service: `[Service](https://developer.android.com/reference/android/app/Service.html)`,` ` id: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` notification: `[Notification](https://developer.android.com/reference/android/app/Notification.html)`,` ` foregroundServiceType: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` [startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification, int))with the third parameter`foregroundServiceType`was added in[Q](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q()). |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int))`(service: `[Service](https://developer.android.com/reference/android/app/Service.html)`, flags: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` 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](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val START_STICKY = 1: Int
```

Constant to return from[onStartCommand](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int)): if this service's process is killed while it is started (after returning from[onStartCommand](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))), 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](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))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](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val STOP_FOREGROUND_DETACH = 2: Int
```

Flag for[stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)): if set, the notification previously provided to[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))will be detached from the service. Only makes sense when[STOP_FOREGROUND_REMOVE](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#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](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#N())and later. It doesn't have any effect on earlier platform versions.  

### STOP_FOREGROUND_REMOVE

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)  

```
const val STOP_FOREGROUND_REMOVE = 1: Int
```

Flag for[stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)): if set, the notification previously provided to[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))will be removed. Otherwise it will remain until a later call (to[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))or[stopForeground](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int))removes it, or the service is destroyed.  

## Public functions

### startForeground

Added in[1.12.0](https://developer.android.com/jetpack/androidx/releases/core#1.12.0)  

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

[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification, int))with the third parameter`foregroundServiceType`was added in[Q](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q()).

Before SDK Version[Q](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q()), this method call should call[startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))without the`foregroundServiceType`parameter.

Beginning with SDK Version[Q](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q()), the allowed foregroundServiceType are:

- [FOREGROUND_SERVICE_TYPE_MANIFEST](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MANIFEST())
- [FOREGROUND_SERVICE_TYPE_NONE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_NONE())
- [FOREGROUND_SERVICE_TYPE_DATA_SYNC](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_DATA_SYNC())
- [FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK())
- [FOREGROUND_SERVICE_TYPE_PHONE_CALL](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_PHONE_CALL())
- [FOREGROUND_SERVICE_TYPE_LOCATION](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_LOCATION())
- [FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE())
- [FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION())
- [FOREGROUND_SERVICE_TYPE_CAMERA](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_CAMERA())
- [FOREGROUND_SERVICE_TYPE_MICROPHONE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MICROPHONE())

Beginning with SDK Version[UPSIDE_DOWN_CAKE](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#UPSIDE_DOWN_CAKE()), apps targeting SDK Version[UPSIDE_DOWN_CAKE](https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#UPSIDE_DOWN_CAKE())is not allowed to use[FOREGROUND_SERVICE_TYPE_NONE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_NONE()). The allowed foregroundServiceType are:

- [FOREGROUND_SERVICE_TYPE_MANIFEST](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MANIFEST())
- [FOREGROUND_SERVICE_TYPE_DATA_SYNC](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_DATA_SYNC())
- [FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK())
- [FOREGROUND_SERVICE_TYPE_PHONE_CALL](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_PHONE_CALL())
- [FOREGROUND_SERVICE_TYPE_LOCATION](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_LOCATION())
- [FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE())
- [FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION())
- [FOREGROUND_SERVICE_TYPE_CAMERA](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_CAMERA())
- [FOREGROUND_SERVICE_TYPE_MICROPHONE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_MICROPHONE())
- [FOREGROUND_SERVICE_TYPE_HEALTH](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_HEALTH())
- [FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING())
- [FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_SYSTEM_EXEMPTED())
- [FOREGROUND_SERVICE_TYPE_SHORT_SERVICE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_SHORT_SERVICE())
- [FOREGROUND_SERVICE_TYPE_SPECIAL_USE](https://developer.android.com/reference/android/content/pm/ServiceInfo.html#FOREGROUND_SERVICE_TYPE_SPECIAL_USE())

|                                                                See also                                                                 |
|-----------------------------------------------------------------------------------------------------------------------------------------|---|
| [startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification))      |   |
| [startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification, int)) |   |

### stopForeground

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/core#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](https://developer.android.com/reference/android/app/Service.html) | service to remove.                                                                                                                                                                                                                                                                               |
| `flags: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | Additional behavior options:[STOP_FOREGROUND_REMOVE](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#STOP_FOREGROUND_REMOVE()),[STOP_FOREGROUND_DETACH](https://developer.android.com/reference/kotlin/androidx/core/app/ServiceCompat#STOP_FOREGROUND_DETACH()). |

|                                                              See also                                                              |
|------------------------------------------------------------------------------------------------------------------------------------|---|
| [startForeground](https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)) |   |