PlaybackPendingIntentBuilder


@UnstableApi
public final class PlaybackPendingIntentBuilder


A builder for creating a PendingIntent for a given Player.Command to be sent to a MediaSessionService.

This builder is primarily intended for creating PendingIntent instances used in home screen widgets (see App widgets overview). For other use cases, such as interacting with a Media3 session service from within an app, it is strongly recommended to use a MediaController or MediaBrowser instead of a PendingIntent.

Summary

Public constructors

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

Creates a builder for a PendingIntent for a given Player.Command.

Public methods

PendingIntent

Builds the PendingIntent.

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

Creates an Intent for a media button event.

static boolean

Returns whether the given Player.Command is supported.

PlaybackPendingIntentBuilder

Sets the additional extras of the Intent.

PlaybackPendingIntentBuilder

Sets the ID of the session.

PlaybackPendingIntentBuilder
@CanIgnoreReturnValue
setStartAsForegroundService(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 PendingIntent for a given Player.Command.

Throws an IllegalArgumentException if the Player.Command is not in the set of supported commands. Supported commands are:

Parameters
Context context

The context.

@Player.Command int command

The Player.Command.

Class<MediaSessionService> serviceClass

The class of the service to which the intent should be sent.

Public methods

build

public PendingIntent build()

Builds the PendingIntent.

Two PendingIntent instances for the same command are considered the same. The key code of the command is used as the request code.

The PendingIntent 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 Intent for a media button event.

Throws an IllegalArgumentException if the Player.Command is not in the set of supported commands. Supported commands are:

Parameters
Context context

The context.

@Player.Command int command

The Player.Command.

@Nullable Bundle extras

The additional extras.

@Nullable String sessionId

The ID of the session or null if the default session ID should be used.

Class<MediaSessionService> serviceClass

The class of the service to which the intent should be sent.

Returns
Intent

The created Intent.

isCommandSupported

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

Returns whether the given Player.Command is supported.

Parameters
@Player.Command int command

The Player.Command.

Returns
boolean

Whether the given Player.Command is supported.

setExtras

@CanIgnoreReturnValue
public PlaybackPendingIntentBuilder setExtras(Bundle extras)

Sets the additional extras of the Intent.

Parameters
Bundle extras

The additional extras.

Returns
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 * setId was not called when building the session.

Parameters
@Nullable String sessionId

The ID of the session as set with setId or null if the default ID was used when building the session.

Returns
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 COMMAND_PLAY_PAUSE command only, hence when getPlayWhenReady is false. The default value is false.

Parameters
boolean startAsForegroundService

Whether the service should be started into the foreground.

Returns
PlaybackPendingIntentBuilder

This builder.