CustomCommandPendingIntentBuilder


@UnstableApi
public final class CustomCommandPendingIntentBuilder


Builds a PendingIntent to send a custom command 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

CustomCommandPendingIntentBuilder(
    Context context,
    Class<MediaSessionService> serviceClass,
    SessionCommand customSessionCommand
)

Creates an instance.

Public methods

PendingIntent

Builds the PendingIntent.

static Intent
createCustomCommandIntent(
    Context context,
    SessionCommand customSessionCommand,
    @Nullable String sessionId,
    Class<MediaSessionService> serviceClass
)

Creates an Intent to send a custom command to a MediaSessionService.

CustomCommandPendingIntentBuilder

Sets the ID of the session.

Public constructors

CustomCommandPendingIntentBuilder

public CustomCommandPendingIntentBuilder(
    Context context,
    Class<MediaSessionService> serviceClass,
    SessionCommand customSessionCommand
)

Creates an instance.

Parameters
Context context

The context.

Class<MediaSessionService> serviceClass

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

SessionCommand customSessionCommand

The custom SessionCommand.

Public methods

build

public PendingIntent build()

Builds the PendingIntent.

Each PendingIntent has a random request code. Any created PendingIntent can be considered different from all other instance.

The PendingIntent is created with the flags PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE.

createCustomCommandIntent

public static Intent createCustomCommandIntent(
    Context context,
    SessionCommand customSessionCommand,
    @Nullable String sessionId,
    Class<MediaSessionService> serviceClass
)

Creates an Intent to send a custom command to a MediaSessionService.

Parameters
Context context

The context.

SessionCommand customSessionCommand

The custom SessionCommand.

@Nullable String sessionId

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

Class<MediaSessionService> serviceClass

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

Returns
Intent

The Intent.

setSessionId

@CanIgnoreReturnValue
public CustomCommandPendingIntentBuilder 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
CustomCommandPendingIntentBuilder

This builder.