# PlaybackPendingIntentBuilder

Artifact: [androidx.media3:media3-session](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/session/PlaybackPendingIntentBuilder.java+class:androidx.media3.session.PlaybackPendingIntentBuilder)

*** ** * ** ***


```
@UnstableApi
public final class PlaybackPendingIntentBuilder
```

<br />

*** ** * ** ***

A builder for creating a `https://developer.android.com/reference/android/app/PendingIntent.html` for a given `https://developer.android.com/reference/androidx/media3/common/Player.Command` to be sent to a `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`.

This builder is primarily intended for creating `https://developer.android.com/reference/android/app/PendingIntent.html` instances used in home screen widgets (see [App widgets overview](https://developer.android.com/develop/ui/views/appwidgets/overview)). For other use cases, such as interacting with a Media3 session service from within an app, it is strongly recommended to use a `https://developer.android.com/reference/androidx/media3/session/MediaController` or `https://developer.android.com/reference/androidx/media3/session/MediaBrowser` instead of a `https://developer.android.com/reference/android/app/PendingIntent.html`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#PlaybackPendingIntentBuilder(android.content.Context,int,java.lang.Class<? extends androidx.media3.session.MediaSessionService>)( https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/media3/common/Player.Command int command, https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/media3/session/MediaSessionService> serviceClass )` Creates a builder for a `https://developer.android.com/reference/android/app/PendingIntent.html` for a given `https://developer.android.com/reference/androidx/media3/common/Player.Command`. |

| ### Public methods |
|---|---|
| `https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#build()()` Builds the `https://developer.android.com/reference/android/app/PendingIntent.html`. |
| `static https://developer.android.com/reference/android/content/Intent.html` | `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#createMediaButtonIntent(android.content.Context,int,android.os.Bundle,java.lang.String,java.lang.Class<? extends androidx.media3.session.MediaSessionService>)( https://developer.android.com/reference/android/content/Context.html context, @https://developer.android.com/reference/androidx/media3/common/Player.Command int command, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html extras, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html sessionId, https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/media3/session/MediaSessionService> serviceClass )` Creates an `https://developer.android.com/reference/android/content/Intent.html` for a media button event. |
| `static boolean` | `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#isCommandSupported(int)(@https://developer.android.com/reference/androidx/media3/common/Player.Command int command)` Returns whether the given `https://developer.android.com/reference/androidx/media3/common/Player.Command` is supported. |
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#setExtras(android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html extras)` Sets the additional extras of the `https://developer.android.com/reference/android/content/Intent.html`. |
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#setSessionId(java.lang.String)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html sessionId)` Sets the ID of the session. |
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | `@https://checkerframework.org/api/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder#setStartAsForegroundService(boolean)(boolean startAsForegroundService)` Sets whether the service should be started into the foreground. |

## Public constructors

### PlaybackPendingIntentBuilder

```
public PlaybackPendingIntentBuilder(
    Context context,
    @Player.Command int command,
    Class<MediaSessionService> serviceClass
)
```

Creates a builder for a `https://developer.android.com/reference/android/app/PendingIntent.html` for a given `https://developer.android.com/reference/androidx/media3/common/Player.Command`.

Throws an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` if the `https://developer.android.com/reference/androidx/media3/common/Player.Command` is not in the set of supported commands. Supported commands are:

- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_PLAY_PAUSE()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_BACK()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_FORWARD()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_NEXT_MEDIA_ITEM()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_NEXT()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_PREVIOUS()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_STOP()`

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context. |
| `@https://developer.android.com/reference/androidx/media3/common/Player.Command int command` | The `https://developer.android.com/reference/androidx/media3/common/Player.Command`. |
| `https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/media3/session/MediaSessionService> serviceClass` | The class of the service to which the intent should be sent. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | for unsupported `https://developer.android.com/reference/androidx/media3/common/Player.Command`. |

## Public methods

### build

```
public PendingIntent build()
```

Builds the `https://developer.android.com/reference/android/app/PendingIntent.html`.

Two `https://developer.android.com/reference/android/app/PendingIntent.html` instances for the same command are `https://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)`. The key code of the command is used as the request code.

The `https://developer.android.com/reference/android/app/PendingIntent.html` is created with the flags `PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_IMMUTABLE`.

### createMediaButtonIntent

```
public static Intent createMediaButtonIntent(
    Context context,
    @Player.Command int command,
    @Nullable Bundle extras,
    @Nullable String sessionId,
    Class<MediaSessionService> serviceClass
)
```

Creates an `https://developer.android.com/reference/android/content/Intent.html` for a media button event.

Throws an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` if the `https://developer.android.com/reference/androidx/media3/common/Player.Command` is not in the set of supported commands. Supported commands are:

- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_PLAY_PAUSE()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_BACK()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_FORWARD()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_NEXT_MEDIA_ITEM()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_NEXT()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_PREVIOUS_MEDIA_ITEM()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_SEEK_TO_PREVIOUS()`
- `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_STOP()`

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context. |
| `@https://developer.android.com/reference/androidx/media3/common/Player.Command int command` | The `https://developer.android.com/reference/androidx/media3/common/Player.Command`. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/os/Bundle.html extras` | The additional extras. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html sessionId` | The ID of the session or null if the default session ID should be used. |
| `https://developer.android.com/reference/java/lang/Class.html<https://developer.android.com/reference/androidx/media3/session/MediaSessionService> serviceClass` | The class of the service to which the intent should be sent. |

| Returns |
|---|---|
| `https://developer.android.com/reference/android/content/Intent.html` | The created `https://developer.android.com/reference/android/content/Intent.html`. |

### isCommandSupported

```
public static boolean isCommandSupported(@Player.Command int command)
```

Returns whether the given `https://developer.android.com/reference/androidx/media3/common/Player.Command` is supported.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/media3/common/Player.Command int command` | The `https://developer.android.com/reference/androidx/media3/common/Player.Command`. |

| Returns |
|---|---|
| `boolean` | Whether the given `https://developer.android.com/reference/androidx/media3/common/Player.Command` is supported. |

### setExtras

```
@CanIgnoreReturnValue
public PlaybackPendingIntentBuilder setExtras(Bundle extras)
```

Sets the additional extras of the `https://developer.android.com/reference/android/content/Intent.html`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/os/Bundle.html extras` | The additional extras. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | This builder. |

### setSessionId

```
@CanIgnoreReturnValue
public PlaybackPendingIntentBuilder setSessionId(@Nullable String sessionId)
```

Sets the ID of the session.

The default value is `null`, which which corresponds to the default ID used when \* `https://developer.android.com/reference/androidx/media3/session/MediaSession.Builder#setId(java.lang.String)` was not called when building the session.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html sessionId` | The ID of the session as set with `https://developer.android.com/reference/androidx/media3/session/MediaSession.Builder#setId(java.lang.String)` or null if the default ID was used when building the session. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | This builder. |

### setStartAsForegroundService

```
@CanIgnoreReturnValue
public PlaybackPendingIntentBuilder setStartAsForegroundService(boolean startAsForegroundService)
```

Sets whether the service should be started into the foreground.

This is usually the case for the `https://developer.android.com/reference/androidx/media3/common/Player#COMMAND_PLAY_PAUSE()` command only, hence when `https://developer.android.com/reference/androidx/media3/common/Player#getPlayWhenReady()` is false. The default value is `false`.

| Parameters |
|---|---|
| `boolean startAsForegroundService` | Whether the service should be started into the foreground. |

| Returns |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/PlaybackPendingIntentBuilder` | This builder. |