# SessionToken

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/SessionToken.java+class:androidx.media3.session.SessionToken)

*** ** * ** ***

<br />

```
public final class SessionToken
```

<br />

*** ** * ** ***

A token that represents an ongoing `https://developer.android.com/reference/androidx/media3/session/MediaSession` or a service (`https://developer.android.com/reference/androidx/media3/session/MediaSessionService`, `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`, or ` androix.media.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 `https://developer.android.com/reference/androidx/media3/session/MediaController` or a `https://developer.android.com/reference/androidx/media3/session/MediaBrowser` to communicate with the session.

It can also be obtained by `https://developer.android.com/reference/androidx/media3/session/SessionToken#getAllServiceTokens(android.content.Context)`.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Documented.html @https://developer.android.com/reference/java/lang/annotation/Retention.html(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html#SOURCE) @https://developer.android.com/reference/java/lang/annotation/Target.html(value = https://developer.android.com/reference/java/lang/annotation/ElementType.html#TYPE_USE) @https://developer.android.com/reference/androidx/annotation/IntDef(value = ) public annotation https://developer.android.com/reference/androidx/media3/session/SessionToken.TokenType` Types of `https://developer.android.com/reference/androidx/media3/session/SessionToken`. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#PLATFORM_SESSION_VERSION() = 0` `https://developer.android.com/reference/androidx/media3/session/SessionToken#getSessionVersion()` for a platform `https://developer.android.com/reference/android/media/session/MediaSession.html` or legacy ` android.support.v4.media.session.MediaSessionCompat`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_LIBRARY_SERVICE() = 2` Type for `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION() = 0` Type for `https://developer.android.com/reference/androidx/media3/session/MediaSession`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION_SERVICE() = 1` Type for `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`. |
| `static final int` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_INTERFACE_VERSION() = 0` Unknown `https://developer.android.com/reference/androidx/media3/session/SessionToken#getInterfaceVersion()` for a `https://developer.android.com/reference/androidx/media3/session/MediaSession` that isn't connected yet, for an older session that didn't publish its interface version, for a platform `https://developer.android.com/reference/android/media/session/MediaSession.html` or for a legacy ` android.support.v4.media.session.MediaSessionCompat`. |
| `static final int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_SESSION_VERSION() = 1000000` Unknown `https://developer.android.com/reference/androidx/media3/session/SessionToken#getSessionVersion()` for a `https://developer.android.com/reference/androidx/media3/session/MediaSession` that isn't connected yet. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/media3/session/SessionToken#SessionToken(android.content.Context,android.content.ComponentName)(https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/android/content/ComponentName.html serviceComponent)` Creates a token for `https://developer.android.com/reference/androidx/media3/session/MediaController` or `https://developer.android.com/reference/androidx/media3/session/MediaBrowser` to connect to one of `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`, `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`, or ` androidx.media.MediaBrowserServiceCompat`. |

| ### Public methods |
|---|---|
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#createSessionToken(android.content.Context,android.media.session.MediaSession.Token)(https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/android/media/session/MediaSession.Token.html token)` Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#createSessionToken(android.content.Context,android.os.Parcelable)(https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/android/os/Parcelable.html token)` Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#createSessionToken(android.content.Context,android.media.session.MediaSession.Token,android.os.Looper)( https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/android/media/session/MediaSession.Token.html token, https://developer.android.com/reference/android/os/Looper.html completionLooper )` Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#createSessionToken(android.content.Context,android.os.Parcelable,android.os.Looper)( https://developer.android.com/reference/android/content/Context.html context, https://developer.android.com/reference/android/os/Parcelable.html token, https://developer.android.com/reference/android/os/Looper.html completionLooper )` Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#equals(java.lang.Object)(@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/Object.html obj)` |
| `static https://developer.android.com/reference/androidx/media3/session/SessionToken` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#fromBundle(android.os.Bundle)(https://developer.android.com/reference/android/os/Bundle.html bundle)` Restores a `SessionToken` from a `https://developer.android.com/reference/android/os/Bundle.html`. |
| `static https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getAllServiceTokens(android.content.Context)(https://developer.android.com/reference/android/content/Context.html context)` Returns an `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html` of `https://developer.android.com/reference/androidx/media3/session/SessionToken` for media session services; `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`, `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`, and `https://developer.android.com/reference/androidx/media/MediaBrowserServiceCompat` regardless of their activeness. |
| `https://developer.android.com/reference/android/os/Bundle.html` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getExtras()()` Returns the extra `https://developer.android.com/reference/android/os/Bundle.html` of this token. |
| `int` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#getInterfaceVersion()()` Returns the `https://developer.android.com/reference/androidx/media3/common/MediaLibraryInfo#INTERFACE_VERSION()` of the session or `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_INTERFACE_VERSION()`. |
| `https://developer.android.com/reference/java/lang/String.html` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#getOriginalPackageName()()` Returns the package name of the application which originally created the session. |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getPackageName()()` Returns the package name of the session |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getServiceName()()` Returns the service name of the session. |
| `int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getSessionVersion()()` Returns the library version of the session, `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_SESSION_VERSION()`, or `https://developer.android.com/reference/androidx/media3/session/SessionToken#PLATFORM_SESSION_VERSION()`. |
| `int` | `@https://developer.android.com/reference/androidx/media3/session/SessionToken.TokenType https://developer.android.com/reference/androidx/media3/session/SessionToken#getType()()` Returns the type of this token. |
| `int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#getUid()()` Returns the UID of the session process, or `https://developer.android.com/reference/androidx/media3/common/C#INDEX_UNSET()` if the UID can't be determined due to missing [package visibility](https://developer.android.com/training/package-visibility). |
| `int` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#hashCode()()` |
| `https://developer.android.com/reference/android/os/Bundle.html` | `@https://developer.android.com/reference/androidx/media3/common/util/UnstableApi https://developer.android.com/reference/androidx/media3/session/SessionToken#toBundle()()` |
| `https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/media3/session/SessionToken#toString()()` |

## Constants

### PLATFORM_SESSION_VERSION

```
public static final int PLATFORM_SESSION_VERSION = 0
```

`https://developer.android.com/reference/androidx/media3/session/SessionToken#getSessionVersion()` for a platform `https://developer.android.com/reference/android/media/session/MediaSession.html` or legacy ` android.support.v4.media.session.MediaSessionCompat`.

### TYPE_LIBRARY_SERVICE

```
public static final int TYPE_LIBRARY_SERVICE = 2
```

Type for `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`.

### TYPE_SESSION

```
public static final int TYPE_SESSION = 0
```

Type for `https://developer.android.com/reference/androidx/media3/session/MediaSession`.

### TYPE_SESSION_SERVICE

```
public static final int TYPE_SESSION_SERVICE = 1
```

Type for `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`.

### UNKNOWN_INTERFACE_VERSION

```
@UnstableApi
public static final int UNKNOWN_INTERFACE_VERSION = 0
```

Unknown `https://developer.android.com/reference/androidx/media3/session/SessionToken#getInterfaceVersion()` for a `https://developer.android.com/reference/androidx/media3/session/MediaSession` that isn't connected yet, for an older session that didn't publish its interface version, for a platform `https://developer.android.com/reference/android/media/session/MediaSession.html` or for a legacy ` android.support.v4.media.session.MediaSessionCompat`.

Note: Use `https://developer.android.com/reference/androidx/media3/session/MediaController#getConnectedToken()` to obtain the version after connecting a controller.

### UNKNOWN_SESSION_VERSION

```
public static final int UNKNOWN_SESSION_VERSION = 1000000
```

Unknown `https://developer.android.com/reference/androidx/media3/session/SessionToken#getSessionVersion()` for a `https://developer.android.com/reference/androidx/media3/session/MediaSession` that isn't connected yet.

Note: Use `https://developer.android.com/reference/androidx/media3/session/MediaController#getConnectedToken()` to obtain the version after connecting a controller.

## Public constructors

### SessionToken

```
public SessionToken(Context context, ComponentName serviceComponent)
```

Creates a token for `https://developer.android.com/reference/androidx/media3/session/MediaController` or `https://developer.android.com/reference/androidx/media3/session/MediaBrowser` to connect to one of `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`, `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`, or ` androidx.media.MediaBrowserServiceCompat`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context. |
| `https://developer.android.com/reference/android/content/ComponentName.html serviceComponent` | The component name of the service. |

## Public methods

### createSessionToken

```
public static ListenableFuture<SessionToken> createSessionToken(Context context, MediaSession.Token token)
```

Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/android/media/session/MediaSession.Token.html token` | The platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the `https://developer.android.com/reference/androidx/media3/session/SessionToken`. |

### createSessionToken

```
@UnstableApi
public static ListenableFuture<SessionToken> createSessionToken(Context context, Parcelable token)
```

Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/android/os/Parcelable.html token` | The `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the `https://developer.android.com/reference/androidx/media3/session/SessionToken`. |

### createSessionToken

```
@UnstableApi
public static ListenableFuture<SessionToken> createSessionToken(
    Context context,
    MediaSession.Token token,
    Looper completionLooper
)
```

Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/android/media/session/MediaSession.Token.html token` | The platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html`. |
| `https://developer.android.com/reference/android/os/Looper.html completionLooper` | The `https://developer.android.com/reference/android/os/Looper.html` on which the returned `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` completes. This `https://developer.android.com/reference/android/os/Looper.html` can't be used to call `future.get()` on the returned `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the `https://developer.android.com/reference/androidx/media3/session/SessionToken`. |

### createSessionToken

```
@UnstableApi
public static ListenableFuture<SessionToken> createSessionToken(
    Context context,
    Parcelable token,
    Looper completionLooper
)
```

Creates a token from a platform `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html`. |
| `https://developer.android.com/reference/android/os/Parcelable.html token` | The `https://developer.android.com/reference/android/media/session/MediaSession.Token.html` or ` android.support.v4.media.session.MediaSessionCompat.Token`. |
| `https://developer.android.com/reference/android/os/Looper.html completionLooper` | The `https://developer.android.com/reference/android/os/Looper.html` on which the returned `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` completes. This `https://developer.android.com/reference/android/os/Looper.html` can't be used to call `future.get()` on the returned `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`. |

| Returns |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/media3/session/SessionToken>` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the `https://developer.android.com/reference/androidx/media3/session/SessionToken`. |

### equals

```
public boolean equals(@Nullable Object obj)
```

### fromBundle

```
@UnstableApi
public static SessionToken fromBundle(Bundle bundle)
```

Restores a `SessionToken` from a `https://developer.android.com/reference/android/os/Bundle.html`.

### getAllServiceTokens

```
public static ImmutableSet<SessionToken> getAllServiceTokens(Context context)
```

Returns an `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/collect/ImmutableSet.html` of `https://developer.android.com/reference/androidx/media3/session/SessionToken` for media session services; `https://developer.android.com/reference/androidx/media3/session/MediaSessionService`, `https://developer.android.com/reference/androidx/media3/session/MediaLibraryService`, and `https://developer.android.com/reference/androidx/media/MediaBrowserServiceCompat` 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](https://developer.android.com/training/package-visibility) 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 `https://developer.android.com/reference/android/os/Bundle.html` of this token.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/media3/session/MediaSession.Builder#setExtras(android.os.Bundle)` |   |

### getInterfaceVersion

```
@UnstableApi
public int getInterfaceVersion()
```

Returns the `https://developer.android.com/reference/androidx/media3/common/MediaLibraryInfo#INTERFACE_VERSION()` of the session or `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_INTERFACE_VERSION()`.

### getOriginalPackageName

```
@UnstableApi
public String getOriginalPackageName()
```

Returns the package name of the application which originally created the session. This may be different from `https://developer.android.com/reference/androidx/media3/session/SessionToken#getPackageName()` if `https://developer.android.com/reference/androidx/media3/session/MediaSession.Builder#setPackageNameOverride(java.lang.String)` was used to create the session.

### 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 `https://developer.android.com/reference/androidx/media3/session/SessionToken#getType()` is `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION()`.

### getSessionVersion

```
public int getSessionVersion()
```

Returns the library version of the session, `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_SESSION_VERSION()`, or `https://developer.android.com/reference/androidx/media3/session/SessionToken#PLATFORM_SESSION_VERSION()`.

- If the session is a platform `https://developer.android.com/reference/android/media/session/MediaSession.html` or legacy ` android.support.v4.media.session.MediaSessionCompat`, this will be `https://developer.android.com/reference/androidx/media3/session/SessionToken#PLATFORM_SESSION_VERSION()`.
- If the token's `https://developer.android.com/reference/androidx/media3/session/SessionToken#getType()` is `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION()`, this will be the same as `https://developer.android.com/reference/androidx/media3/common/MediaLibraryInfo#VERSION_INT()` of the session.
- If the token's `https://developer.android.com/reference/androidx/media3/session/SessionToken#getType()` is `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION_SERVICE()` or `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_LIBRARY_SERVICE()`, this will be `https://developer.android.com/reference/androidx/media3/session/SessionToken#UNKNOWN_SESSION_VERSION()`. You can obtain the actual session version after a connecting a controller via the `https://developer.android.com/reference/androidx/media3/session/MediaController#getConnectedToken()` of type `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION()`.
- 

### getType

```
@SessionToken.TokenType
public int getType()
```

Returns the type of this token. One of `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION()`, `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_SESSION_SERVICE()`, or `https://developer.android.com/reference/androidx/media3/session/SessionToken#TYPE_LIBRARY_SERVICE()`.

### getUid

```
public int getUid()
```

Returns the UID of the session process, or `https://developer.android.com/reference/androidx/media3/common/C#INDEX_UNSET()` if the UID can't be determined due to missing [package visibility](https://developer.android.com/training/package-visibility).

### hashCode

```
public int hashCode()
```

### toBundle

```
@UnstableApi
public Bundle toBundle()
```

### toString

```
public String toString()
```