Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/media/session/MediaSessionManager#nestedclasses) \| [Methods](https://developer.android.com/reference/android/media/session/MediaSessionManager#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/session/MediaSessionManager#inhmethods)

# MediaSessionManager

*** ** * ** ***

` public final class MediaSessionManager `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.session.MediaSessionManager |

<br />

*** ** * ** ***

Provides support for interacting with `https://developer.android.com/reference/android/media/session/MediaSession` that applications have published to express their ongoing media playback state.
**See also:**

- `https://developer.android.com/reference/android/media/session/MediaSession`
- `https://developer.android.com/reference/android/media/session/MediaController`

## Summary

| ### Nested classes ||
|---|---|
| ` interface` | `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener` Listens for changes to the list of active sessions. |
| ` interface` | `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnMediaKeyEventSessionChangedListener` Listener to receive changes in the media key event session, which would receive a media key event unless specified. |
| ` interface` | `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnSession2TokensChangedListener` *This interface was deprecated in API level 37. `https://developer.android.com/reference/android/media/MediaSession2` is deprecated.* |
| ` class` | `https://developer.android.com/reference/android/media/session/MediaSessionManager.RemoteUserInfo` Information of a remote user of `https://developer.android.com/reference/android/media/session/MediaSession` or `https://developer.android.com/reference/android/service/media/MediaBrowserService`. |

| ### Public methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener,%20android.content.ComponentName)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener, https://developer.android.com/reference/android/content/ComponentName notificationListener) ` Add a listener to be notified when the list of active sessions changes. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener,%20android.content.ComponentName,%20android.os.Handler)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener, https://developer.android.com/reference/android/content/ComponentName notificationListener, https://developer.android.com/reference/android/os/Handler handler) ` Add a listener to be notified when the list of active sessions changes. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnActiveSessionsForPackageChangedListener(java.lang.String,%20java.util.concurrent.Executor,%20android.content.ComponentName,%20android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/util/concurrent/Executor executor, https://developer.android.com/reference/android/content/ComponentName notificationListener, https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener) ` Add a listener to be notified when the list of active controllers changes for the `packageName`, as returned by `https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String)`. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnActiveSessionsForPackageChangedListener(java.lang.String,%20java.util.concurrent.Executor,%20android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/java/util/concurrent/Executor executor, https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener) ` Add a listener to be notified when the list of active sessions changes for the `packageName`, as returned by `https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String)`. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnMediaKeyEventSessionChangedListener(java.util.concurrent.Executor,%20android.media.session.MediaSessionManager.OnMediaKeyEventSessionChangedListener)(https://developer.android.com/reference/java/util/concurrent/Executor executor, https://developer.android.com/reference/android/media/session/MediaSessionManager.OnMediaKeyEventSessionChangedListener listener) ` Add a listener to be notified when the media key session is changed. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnSession2TokensChangedListener(android.media.session.MediaSessionManager.OnSession2TokensChangedListener)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnSession2TokensChangedListener listener) ` *This method was deprecated in API level 37. `https://developer.android.com/reference/android/media/MediaSession2` is deprecated.* |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#addOnSession2TokensChangedListener(android.media.session.MediaSessionManager.OnSession2TokensChangedListener,%20android.os.Handler)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnSession2TokensChangedListener listener, https://developer.android.com/reference/android/os/Handler handler) ` *This method was deprecated in API level 37. `https://developer.android.com/reference/android/media/MediaSession2` is deprecated.* |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaController>` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessions(android.content.ComponentName)(https://developer.android.com/reference/android/content/ComponentName notificationListener) ` Get a list of controllers for all active sessions. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaSession.Token>` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String)(https://developer.android.com/reference/java/lang/String packageName) ` Get a list of `https://developer.android.com/reference/android/media/session/MediaSession.Token` for all active sessions for the `packageName`. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaSession.Token>` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String,%20android.content.ComponentName)(https://developer.android.com/reference/java/lang/String packageName, https://developer.android.com/reference/android/content/ComponentName notificationListener) ` Get a list of `https://developer.android.com/reference/android/media/session/MediaSession.Token` for all active sessions for the `packageName`. |
| ` https://developer.android.com/reference/android/media/session/MediaSession.Token` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getMediaKeyEventSession()() ` Gets the media key event session, which would receive a media key event unless specified. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getMediaKeyEventSessionPackageName()() ` Gets the package name of the media key event session. |
| ` https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/Session2Token>` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#getSession2Tokens()() ` *This method was deprecated in API level 37. `https://developer.android.com/reference/android/media/MediaSession2` is deprecated.* |
| ` boolean` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#isTrustedForMediaControl(android.media.session.MediaSessionManager.RemoteUserInfo)(https://developer.android.com/reference/android/media/session/MediaSessionManager.RemoteUserInfo userInfo) ` Checks whether the remote user is a trusted app. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#notifySession2Created(android.media.Session2Token)(https://developer.android.com/reference/android/media/Session2Token token) ` *This method was deprecated in API level 31. Don't use this method. A new media session is notified automatically.* |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener) ` Stop receiving active sessions updates on the specified listener. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnActiveSessionsForPackageChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnActiveSessionsChangedListener sessionListener) ` Stop receiving active sessions updates on the specified listener for package. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnMediaKeyEventSessionChangedListener(android.media.session.MediaSessionManager.OnMediaKeyEventSessionChangedListener)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnMediaKeyEventSessionChangedListener listener) ` Stop receiving updates on media key event session change on the specified listener. |
| ` void` | ` https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnSession2TokensChangedListener(android.media.session.MediaSessionManager.OnSession2TokensChangedListener)(https://developer.android.com/reference/android/media/session/MediaSessionManager.OnSession2TokensChangedListener listener) ` *This method was deprecated in API level 37. `https://developer.android.com/reference/android/media/MediaSession2` is deprecated.* |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### addOnActiveSessionsChangedListener

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener, 
                ComponentName notificationListener)
```

Add a listener to be notified when the list of active sessions changes.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app. You may also retrieve this list if your app is an enabled notificationlistener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs, in which case you must pass the `https://developer.android.com/reference/android/content/ComponentName` of your enabled listener.

<br />

| Parameters ||
|---|---|
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to add. This value cannot be `null`. <br /> |
| `notificationListener` | `ComponentName`: The enabled notification listener component. May be null. <br /> |

### addOnActiveSessionsChangedListener

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener, 
                ComponentName notificationListener, 
                Handler handler)
```

Add a listener to be notified when the list of active sessions changes.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app. You may also retrieve this list if your app is an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs, in which case you must pass the `https://developer.android.com/reference/android/content/ComponentName` of your enabled listener. Updates will be posted to the handler specified or to the caller's thread if the handler is null.

<br />

| Parameters ||
|---|---|
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to add. This value cannot be `null`. <br /> |
| `notificationListener` | `ComponentName`: The enabled notification listener component. May be null. <br /> |
| `handler` | `Handler`: The handler to post events to. This value may be `null`. <br /> |

### addOnActiveSessionsForPackageChangedListener

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnActiveSessionsForPackageChangedListener (String packageName, 
                Executor executor, 
                ComponentName notificationListener, 
                MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
```

Add a listener to be notified when the list of active controllers changes for the `packageName`, as returned by `https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String)`. The registered listener can be removed by calling `https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnActiveSessionsForPackageChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)`.

If the `packageName` is different from the `https://developer.android.com/reference/android/content/Context#getPackageName()`, it requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app. You may also attach a listener if your app is an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs, in which case you must pass the `https://developer.android.com/reference/android/content/ComponentName` of your enabled listener.

<br />

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package name filter to be applied for the listener. This value cannot be `null`. <br /> |
| `executor` | `Executor`: The executor to be used for sending events to listener. This value cannot be `null`. Callback and listener events are dispatched through this `https://developer.android.com/reference/java/util/concurrent/Executor`, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use `https://developer.android.com/reference/android/content/Context#getMainExecutor()`. Otherwise, provide an `https://developer.android.com/reference/java/util/concurrent/Executor` that dispatches to an appropriate thread. <br /> |
| `notificationListener` | `ComponentName`: The enabled notification listener component. May be null. <br /> |
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to add. This value cannot be `null`. <br /> |

### addOnActiveSessionsForPackageChangedListener

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnActiveSessionsForPackageChangedListener (String packageName, 
                Executor executor, 
                MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
```

Add a listener to be notified when the list of active sessions changes for the `packageName`, as returned by `https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessionsForPackage(java.lang.String)`. The registered listener can be removed by calling `https://developer.android.com/reference/android/media/session/MediaSessionManager#removeOnActiveSessionsForPackageChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)`.

If the `packageName` is different from the `https://developer.android.com/reference/android/content/Context#getPackageName()`, it requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app.

<br />

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: The package name filter to be applied for the listener. This value cannot be `null`. <br /> |
| `executor` | `Executor`: The executor to be used for sending events to listener. This value cannot be `null`. Callback and listener events are dispatched through this `https://developer.android.com/reference/java/util/concurrent/Executor`, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use `https://developer.android.com/reference/android/content/Context#getMainExecutor()`. Otherwise, provide an `https://developer.android.com/reference/java/util/concurrent/Executor` that dispatches to an appropriate thread. <br /> |
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to add. This value cannot be `null`. <br /> |

### addOnMediaKeyEventSessionChangedListener

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnMediaKeyEventSessionChangedListener (Executor executor, 
                MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
```

Add a listener to be notified when the media key session is changed.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app, or the app has an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs. If none of them applies, it will throw a `https://developer.android.com/reference/java/lang/SecurityException`.

<br />

| Parameters ||
|---|---|
| `executor` | `Executor`: The executor on which the listener should be invoked. This value cannot be `null`. Callback and listener events are dispatched through this `https://developer.android.com/reference/java/util/concurrent/Executor`, providing an easy way to control which thread is used. To dispatch events through the main thread of your application, you can use `https://developer.android.com/reference/android/content/Context#getMainExecutor()`. Otherwise, provide an `https://developer.android.com/reference/java/util/concurrent/Executor` that dispatches to an appropriate thread. <br /> |
| `listener` | `MediaSessionManager.OnMediaKeyEventSessionChangedListener`: A `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnMediaKeyEventSessionChangedListener`. This value cannot be `null`. <br /> |

### addOnSession2TokensChangedListener

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)
```

<br />

**This method was deprecated in API level 37.**   
`https://developer.android.com/reference/android/media/MediaSession2` is deprecated.

Adds a listener to be notified when the `https://developer.android.com/reference/android/media/session/MediaSessionManager#getSession2Tokens()` changes.

This API is not generally intended for third party application developers. Apps wanting media session functionality should use the [AndroidX Media3 Session Library](https://developer.android.com/reference/androidx/media3/session/package-summary).

<br />

| Parameters ||
|---|---|
| `listener` | `MediaSessionManager.OnSession2TokensChangedListener`: The listener to add. This value cannot be `null`. <br /> |

### addOnSession2TokensChangedListener

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener, 
                Handler handler)
```

<br />

**This method was deprecated in API level 37.**   
`https://developer.android.com/reference/android/media/MediaSession2` is deprecated.

Adds a listener to be notified when the `https://developer.android.com/reference/android/media/session/MediaSessionManager#getSession2Tokens()` changes.

This API is not generally intended for third party application developers. Apps wanting media session functionality should use the [AndroidX Media3 Session Library](https://developer.android.com/reference/androidx/media3/session/package-summary).

<br />

| Parameters ||
|---|---|
| `listener` | `MediaSessionManager.OnSession2TokensChangedListener`: The listener to add. This value cannot be `null`. <br /> |
| `handler` | `Handler`: The handler to call listener on. This value cannot be `null`. <br /> |

### getActiveSessions

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<MediaController> getActiveSessions (ComponentName notificationListener)
```

Get a list of controllers for all active sessions. The controllers will be provided in priority order with the most important controller at index 0.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app. You may also retrieve this list if your app is an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs, in which case you must pass the `https://developer.android.com/reference/android/content/ComponentName` of your enabled listener.

<br />

| Parameters ||
|---|---|
| `notificationListener` | `ComponentName`: The enabled notification listener component. May be null. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaController>` | A list of controllers for active sessions. This value cannot be `null`. <br /> |

### getActiveSessionsForPackage

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<MediaSession.Token> getActiveSessionsForPackage (String packageName)
```

Get a list of `https://developer.android.com/reference/android/media/session/MediaSession.Token` for all active sessions for the `packageName`. The tokens will be provided in priority order with the most important controller at index 0.

If the `packageName` is provided and is different from `https://developer.android.com/reference/android/content/Context#getPackageName()`, this requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app.

<br />

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: Package name of the application for which tokens should be fetched. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaSession.Token>` | A list of `https://developer.android.com/reference/android/media/session/MediaSession.Token` for active sessions of the calling application. This value cannot be `null`. <br /> |

### getActiveSessionsForPackage

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<MediaSession.Token> getActiveSessionsForPackage (String packageName, 
                ComponentName notificationListener)
```

Get a list of `https://developer.android.com/reference/android/media/session/MediaSession.Token` for all active sessions for the `packageName`. The tokens will be provided in priority order with the most important controller at index 0.

If the `packageName` is provided and is different from `https://developer.android.com/reference/android/content/Context#getPackageName()`, this requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app. You may also retrieve this list if your app is an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs, in which case you must pass the `https://developer.android.com/reference/android/content/ComponentName` of your enabled listener. If none of them applies, it will throw a `https://developer.android.com/reference/java/lang/SecurityException`.

<br />

<br />

| Parameters ||
|---|---|
| `packageName` | `String`: Package name of the application for which tokens should be fetched. This value cannot be `null`. <br /> |
| `notificationListener` | `ComponentName`: The enabled notification listener component. May be null. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/session/MediaSession.Token>` | A list of controllers for active sessions of the calling application. This value cannot be `null`. <br /> |

### getMediaKeyEventSession

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public MediaSession.Token getMediaKeyEventSession ()
```

Gets the media key event session, which would receive a media key event unless specified.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app, or the app has an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs. If none of them applies, it will throw a `https://developer.android.com/reference/java/lang/SecurityException`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/session/MediaSession.Token` | The media key event session, which would receive key events by default, unless the caller has specified the target. Can be `null`. <br /> |

### getMediaKeyEventSessionPackageName

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String getMediaKeyEventSessionPackageName ()
```

Gets the package name of the media key event session.

This requires the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission be held by the calling app, or the app has an enabled notification listener using the `https://developer.android.com/reference/android/service/notification/NotificationListenerService` APIs. If none of them applies, it will throw a `https://developer.android.com/reference/java/lang/SecurityException`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | The package name of the media key event session or the last session's media button receiver if the media key event session is `null`. Returns an empty string if neither of them exists. <br /> |

**See also:**

- `https://developer.android.com/reference/android/media/session/MediaSessionManager#getMediaKeyEventSession()`

### getSession2Tokens

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public List<Session2Token> getSession2Tokens ()
```

<br />

**This method was deprecated in API level 37.**   
`https://developer.android.com/reference/android/media/MediaSession2` is deprecated.

Gets a list of `https://developer.android.com/reference/android/media/Session2Token` with type `https://developer.android.com/reference/android/media/Session2Token#TYPE_SESSION` for the current user.

Although this API can be used without any restriction, each session owners can accept or reject your uses of `https://developer.android.com/reference/android/media/MediaSession2`.

This API is not generally intended for third party application developers. Apps wanting media session functionality should use the [AndroidX Media3 Session Library](https://developer.android.com/reference/androidx/media3/session/package-summary).

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/util/List<https://developer.android.com/reference/android/media/Session2Token>` | A list of `https://developer.android.com/reference/android/media/Session2Token`. This value cannot be `null`. <br /> |

### isTrustedForMediaControl

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public boolean isTrustedForMediaControl (MediaSessionManager.RemoteUserInfo userInfo)
```

Checks whether the remote user is a trusted app.

An app is trusted if the app holds the `https://developer.android.com/reference/android/Manifest.permission#MEDIA_CONTENT_CONTROL` permission or has an enabled notification listener.

<br />

| Parameters ||
|---|---|
| `userInfo` | `MediaSessionManager.RemoteUserInfo`: The remote user info from either `https://developer.android.com/reference/android/media/session/MediaSession#getCurrentControllerInfo()` or `https://developer.android.com/reference/android/service/media/MediaBrowserService#getCurrentBrowserInfo()`. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | `true` if the remote user is trusted and its package name matches with the UID. `false` otherwise. <br /> |

### notifySession2Created

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void notifySession2Created (Session2Token token)
```

<br />

**This method was deprecated in API level 31.**   

Don't use this method. A new media session is notified automatically.

Notifies that a new `https://developer.android.com/reference/android/media/MediaSession2` with type `https://developer.android.com/reference/android/media/Session2Token#TYPE_SESSION` is created.

Do not use this API directly, but create a new instance through the `https://developer.android.com/reference/android/media/MediaSession2.Builder` instead.

<br />

| Parameters ||
|---|---|
| `token` | `Session2Token`: newly created session2 token. This value cannot be `null`. <br /> |

### removeOnActiveSessionsChangedListener

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
```

Stop receiving active sessions updates on the specified listener.

<br />

| Parameters ||
|---|---|
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to remove. This value cannot be `null`. <br /> |

### removeOnActiveSessionsForPackageChangedListener

Added in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeOnActiveSessionsForPackageChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
```

Stop receiving active sessions updates on the specified listener for package.

<br />

| Parameters ||
|---|---|
| `sessionListener` | `MediaSessionManager.OnActiveSessionsChangedListener`: The listener to remove. This value cannot be `null`. <br /> |

### removeOnMediaKeyEventSessionChangedListener

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeOnMediaKeyEventSessionChangedListener (MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
```

Stop receiving updates on media key event session change on the specified listener.

<br />

| Parameters ||
|---|---|
| `listener` | `MediaSessionManager.OnMediaKeyEventSessionChangedListener`: A `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnMediaKeyEventSessionChangedListener`. This value cannot be `null`. <br /> |

### removeOnSession2TokensChangedListener

Added in [API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)   
Deprecated in [API level 37](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void removeOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)
```

<br />

**This method was deprecated in API level 37.**   
`https://developer.android.com/reference/android/media/MediaSession2` is deprecated.

Removes the `https://developer.android.com/reference/android/media/session/MediaSessionManager.OnSession2TokensChangedListener` to stop receiving session token updates.

This API is not generally intended for third party application developers. Apps wanting media session functionality should use the [AndroidX Media3 Session Library](https://developer.android.com/reference/androidx/media3/session/package-summary).

<br />

| Parameters ||
|---|---|
| `listener` | `MediaSessionManager.OnSession2TokensChangedListener`: The listener to remove. This value cannot be `null`. <br /> |