public final class SessionToken implements Bundleable


A token that represents an ongoing MediaSession or a service (, MediaLibraryService, or MediaBrowserServiceCompat). If it represents a service, it may not be ongoing.

This may be passed to apps by the session owner to allow them to create a or a MediaBrowser to communicate with the session.

It can also be obtained by getAllServiceTokens.

Summary

Nested types

@Documented
@Retention(value = RetentionPolicy.SOURCE)
@Target(value = TYPE_USE)
@IntDef(value = )
public annotation SessionToken.TokenType

Types of SessionToken.

Constants

static final Bundleable.Creator<SessionToken>

This field is deprecated.

Use fromBundle instead.

static final int

Type for MediaLibraryService.

static final int

Type for MediaSession.

static final int

Type for MediaSessionService.

Public constructors

SessionToken(Context context, ComponentName serviceComponent)

Creates a token for MediaController or MediaBrowser to connect to one of , MediaLibraryService, or MediaBrowserServiceCompat.

Public methods

static ListenableFuture<SessionToken>
@UnstableApi
createSessionToken(
    Context context,
    MediaSessionCompat.Token compatToken
)

Creates a token from a MediaSessionCompat.Token.

static ListenableFuture<SessionToken>

Creates a token from a android.media.session.MediaSession.Token.

static ListenableFuture<SessionToken>
@UnstableApi
createSessionToken(
    Context context,
    MediaSessionCompat.Token compatToken,
    Looper completionLooper
)

Creates a token from a MediaSessionCompat.Token.

static ListenableFuture<SessionToken>
@UnstableApi
@RequiresApi(value = 21)
createSessionToken(
    Context context,
    MediaSession.Token token,
    Looper completionLooper
)

Creates a token from a android.media.session.MediaSession.Token.

boolean
static SessionToken

Restores a SessionToken from a Bundle.

static ImmutableSet<SessionToken>

Returns an ImmutableSet of session tokens for media session services; MediaSessionService, MediaLibraryService, and regardless of their activeness.

Bundle

Returns the extra Bundle of this token.

int

Returns the interface version of the session if the type is TYPE_SESSION.

String

Returns the package name of the session

String

Returns the service name of the session.

int

Returns the library version of the session if the type is TYPE_SESSION.

int

Returns the type of this token.

int

Returns the UID of the session process, or INDEX_UNSET if the UID can't be determined due to missing package visibility.

int
Bundle

Returns a Bundle representing the information stored in this object.

String

Constants

CREATOR

@UnstableApi
public static final Bundleable.Creator<SessionTokenCREATOR

Object that can restore SessionToken from a Bundle.

TYPE_LIBRARY_SERVICE

public static final int TYPE_LIBRARY_SERVICE = 2

Type for MediaLibraryService.

TYPE_SESSION

public static final int TYPE_SESSION = 0

Type for MediaSession.

TYPE_SESSION_SERVICE

public static final int TYPE_SESSION_SERVICE = 1

Type for MediaSessionService.

Public constructors

SessionToken

public SessionToken(Context context, ComponentName serviceComponent)

Creates a token for MediaController or MediaBrowser to connect to one of , MediaLibraryService, or MediaBrowserServiceCompat.

Parameters
Context context

The context.

ComponentName serviceComponent

The component name of the service.

Public methods

createSessionToken

@UnstableApi
public static ListenableFuture<SessionTokencreateSessionToken(
    Context context,
    MediaSessionCompat.Token compatToken
)

Creates a token from a MediaSessionCompat.Token.

Parameters
Context context

A Context.

MediaSessionCompat.Token compatToken

The MediaSessionCompat.Token.

createSessionToken

@UnstableApi
public static ListenableFuture<SessionTokencreateSessionToken(
    Context context,
    MediaSessionCompat.Token compatToken,
    Looper completionLooper
)

Creates a token from a MediaSessionCompat.Token.

Parameters
Context context

A Context.

MediaSessionCompat.Token compatToken

The MediaSessionCompat.Token.

Looper completionLooper

The Looper on which the returned ListenableFuture completes. This Looper can't be used to call future.get() on the returned ListenableFuture.

createSessionToken

@UnstableApi
@RequiresApi(value = 21)
public static ListenableFuture<SessionTokencreateSessionToken(
    Context context,
    MediaSession.Token token,
    Looper completionLooper
)

Creates a token from a android.media.session.MediaSession.Token.

Parameters
Context context

A Context.

MediaSession.Token token

The android.media.session.MediaSession.Token.

Looper completionLooper

The Looper on which the returned ListenableFuture completes. This Looper can't be used to call future.get() on the returned ListenableFuture.

equals

public boolean equals(@Nullable Object obj)

fromBundle

@UnstableApi
public static SessionToken fromBundle(Bundle bundle)

Restores a SessionToken from a Bundle.

getAllServiceTokens

public static ImmutableSet<SessionTokengetAllServiceTokens(Context context)

Returns an ImmutableSet of session tokens for media session services; MediaSessionService, MediaLibraryService, and regardless of their activeness.

The app targeting API level 30 or higher must include a <queries> element in their manifest to get service tokens of other apps. See the following example and this guide for more information.

<intent>
  <action android:name="androidx.media3.session.MediaSessionService" />
</intent>
<intent>
  <action android:name="androidx.media3.session.MediaLibraryService" />
</intent>
<intent>
  <action android:name="android.media.browse.MediaBrowserService" />
</intent>

getExtras

public Bundle getExtras()

Returns the extra Bundle of this token.

See also
setExtras

getInterfaceVersion

@UnstableApi
public int getInterfaceVersion()

Returns the interface version of the session if the type is TYPE_SESSION. Otherwise, it returns 0.

getPackageName

public String getPackageName()

Returns the package name of the session

getServiceName

public String getServiceName()

Returns the service name of the session. It will be an empty string if the type is TYPE_SESSION.

getSessionVersion

public int getSessionVersion()

Returns the library version of the session if the type is TYPE_SESSION. Otherwise, it returns 0.

It will be the same as VERSION_INT of the session, or less than 1000000 if the session is a legacy session.

getType

@SessionToken.TokenType
public int getType()

Returns the type of this token. One of TYPE_SESSION, TYPE_SESSION_SERVICE, or TYPE_LIBRARY_SERVICE.

getUid

public int getUid()

Returns the UID of the session process, or INDEX_UNSET if the UID can't be determined due to missing package visibility.

hashCode

public int hashCode()

toBundle

@UnstableApi
public Bundle toBundle()

Returns a Bundle representing the information stored in this object.

toString

public String toString()