# 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)

*** ** * ** ***


```
public final class ServiceCompat
```

<br />

*** ** * ** ***

Helper for accessing features in `https://developer.android.com/reference/android/app/Service.html`.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/app/ServiceCompat#START_STICKY() = 1` Constant to return from `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 `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. |
| `static final int` | `https://developer.android.com/reference/androidx/core/app/ServiceCompat#STOP_FOREGROUND_DETACH() = 2` Flag for `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)`: if set, the notification previously provided to `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)` will be detached from the service. |
| `static final int` | `https://developer.android.com/reference/androidx/core/app/ServiceCompat#STOP_FOREGROUND_REMOVE() = 1` Flag for `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)`: if set, the notification previously provided to `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)` will be removed. |

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/core/app/ServiceCompat#startForeground(android.app.Service,int,android.app.Notification,int)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Service.html service, int id, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Notification.html notification, int foregroundServiceType )` `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification, int)` with the third parameter `foregroundServiceType` was added in `https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#Q`. |
| `static void` | `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Service.html 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](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int START_STICKY = 1
```

Constant to return from `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 `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 `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)

```
public static final int STOP_FOREGROUND_DETACH = 2
```

Flag for `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)`: if set, the notification previously provided to `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)` will be detached from the service. Only makes sense when `https://developer.android.com/reference/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 `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)

```
public static final int STOP_FOREGROUND_REMOVE = 1
```

Flag for `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)`: if set, the notification previously provided to `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 `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)` or `https://developer.android.com/reference/androidx/core/app/ServiceCompat#stopForeground(android.app.Service,int)` removes it, or the service is destroyed.

## Public methods

### startForeground

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

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

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

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

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

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

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

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

| See also |
|---|---|
| `https://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)` |   |
| `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)

```
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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/app/Service.html service` | service to remove. |
| `int flags` | Additional behavior options: `https://developer.android.com/reference/androidx/core/app/ServiceCompat#STOP_FOREGROUND_REMOVE()`, `https://developer.android.com/reference/androidx/core/app/ServiceCompat#STOP_FOREGROUND_DETACH()`. |

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