MediaSessionManager
public
final
class
MediaSessionManager
extends Object
| java.lang.Object | |
| ↳ | android.media.session.MediaSessionManager |
Provides support for interacting with media sessions
that applications have published to express their ongoing media playback
state.
See also:
Summary
Nested classes | |
|---|---|
interface |
MediaSessionManager.OnActiveSessionsChangedListener
Listens for changes to the list of active sessions. |
interface |
MediaSessionManager.OnMediaKeyEventSessionChangedListener
Listener to receive changes in the media key event session, which would receive a media key event unless specified. |
interface |
MediaSessionManager.OnSession2TokensChangedListener
This interface is deprecated.
|
class |
MediaSessionManager.RemoteUserInfo
Information of a remote user of |
Public methods | |
|---|---|
void
|
addOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener, ComponentName notificationListener)
Add a listener to be notified when the list of active sessions changes. |
void
|
addOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener, ComponentName notificationListener, Handler handler)
Add a listener to be notified when the list of active sessions changes. |
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 |
void
|
addOnActiveSessionsForPackageChangedListener(String packageName, Executor executor, MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
Add a listener to be notified when the list of active sessions changes for the |
void
|
addOnMediaKeyEventSessionChangedListener(Executor executor, MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
Add a listener to be notified when the media key session is changed. |
void
|
addOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener)
This method is deprecated.
|
void
|
addOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener, Handler handler)
This method is deprecated.
|
List<MediaController>
|
getActiveSessions(ComponentName notificationListener)
Get a list of controllers for all active sessions. |
List<MediaSession.Token>
|
getActiveSessionsForPackage(String packageName)
Get a list of |
List<MediaSession.Token>
|
getActiveSessionsForPackage(String packageName, ComponentName notificationListener)
Get a list of |
MediaSession.Token
|
getMediaKeyEventSession()
Gets the media key event session, which would receive a media key event unless specified. |
String
|
getMediaKeyEventSessionPackageName()
Gets the package name of the media key event session. |
List<Session2Token>
|
getSession2Tokens()
This method is deprecated.
|
boolean
|
isTrustedForMediaControl(MediaSessionManager.RemoteUserInfo userInfo)
Checks whether the remote user is a trusted app. |
void
|
notifySession2Created(Session2Token token)
This method was deprecated in API level 31. Don't use this method. A new media session is notified automatically. |
void
|
removeOnActiveSessionsChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
Stop receiving active sessions updates on the specified listener. |
void
|
removeOnActiveSessionsForPackageChangedListener(MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
Stop receiving active sessions updates on the specified listener for package. |
void
|
removeOnMediaKeyEventSessionChangedListener(MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
Stop receiving updates on media key event session change on the specified listener. |
void
|
removeOnSession2TokensChangedListener(MediaSessionManager.OnSession2TokensChangedListener listener)
This method is deprecated.
|
Inherited methods | |
|---|---|
Public methods
addOnActiveSessionsChangedListener
public void addOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener, ComponentName notificationListener)
Add a listener to be notified when the list of active sessions changes.
This requires the 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 NotificationListenerService APIs, in which case you
must pass the ComponentName of your enabled listener.
| Parameters | |
|---|---|
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to add.
This value cannot be null. |
notificationListener |
ComponentName: The enabled notification listener component. May be null. |
addOnActiveSessionsChangedListener
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 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 NotificationListenerService APIs, in which case you
must pass the ComponentName of your enabled listener. Updates will be posted to the
handler specified or to the caller's thread if the handler is null.
| Parameters | |
|---|---|
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to add.
This value cannot be null. |
notificationListener |
ComponentName: The enabled notification listener component. May be null. |
handler |
Handler: The handler to post events to.
This value may be null. |
addOnActiveSessionsForPackageChangedListener
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 getActiveSessionsForPackage(String) (String)}. The
registered listener can be removed by calling MediaSessionManager.removeOnActiveSessionsForPackageChangedListener.
If the packageName is different from the Context.getPackageName(), it
requires the 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 NotificationListenerService APIs, in which case you must pass the
ComponentName of your enabled listener.
| Parameters | |
|---|---|
packageName |
String: The package name filter to be applied for the listener.
This value cannot be null. |
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 Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
notificationListener |
ComponentName: The enabled notification listener component. May be null. |
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to add.
This value cannot be null. |
addOnActiveSessionsForPackageChangedListener
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 getActiveSessionsForPackage(String) (String)}. The
registered listener can be removed by calling MediaSessionManager.removeOnActiveSessionsForPackageChangedListener.
If the packageName is different from the Context.getPackageName(), it
requires the Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by
the calling app.
| Parameters | |
|---|---|
packageName |
String: The package name filter to be applied for the listener.
This value cannot be null. |
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 Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to add.
This value cannot be null. |
addOnMediaKeyEventSessionChangedListener
public void addOnMediaKeyEventSessionChangedListener (Executor executor, MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
Add a listener to be notified when the media key session is changed.
This requires the Manifest.permission.MEDIA_CONTENT_CONTROL
permission be held by the calling app, or the app has an enabled notification listener
using the NotificationListenerService APIs. If none of them applies, it will throw
a SecurityException.
| 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 Executor, providing an easy way to control which thread is
used. To dispatch events through the main thread of your
application, you can use
Context.getMainExecutor().
Otherwise, provide an Executor that dispatches to an appropriate thread. |
listener |
MediaSessionManager.OnMediaKeyEventSessionChangedListener: A OnMediaKeyEventSessionChangedListener.
This value cannot be null. |
addOnSession2TokensChangedListener
public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)
This method is deprecated.
MediaSession2 is deprecated.
Adds a listener to be notified when the 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.
| Parameters | |
|---|---|
listener |
MediaSessionManager.OnSession2TokensChangedListener: The listener to add.
This value cannot be null. |
addOnSession2TokensChangedListener
public void addOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener, Handler handler)
This method is deprecated.
MediaSession2 is deprecated.
Adds a listener to be notified when the 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.
| Parameters | |
|---|---|
listener |
MediaSessionManager.OnSession2TokensChangedListener: The listener to add.
This value cannot be null. |
handler |
Handler: The handler to call listener on.
This value cannot be null. |
getActiveSessions
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 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
NotificationListenerService APIs, in which case you must pass the
ComponentName of your enabled listener.
| Parameters | |
|---|---|
notificationListener |
ComponentName: The enabled notification listener component.
May be null. |
| Returns | |
|---|---|
List<MediaController> |
A list of controllers for active sessions.
This value cannot be null. |
getActiveSessionsForPackage
public List<MediaSession.Token> getActiveSessionsForPackage (String packageName)
Get a list of 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 Context.getPackageName(), this requires the Manifest.permission.MEDIA_CONTENT_CONTROL permission be held by the calling app.
| Parameters | |
|---|---|
packageName |
String: Package name of the application for which tokens should be fetched.
This value cannot be null. |
| Returns | |
|---|---|
List<MediaSession.Token> |
A list of MediaSession.Token for active sessions of the calling application.
This value cannot be null. |
getActiveSessionsForPackage
public List<MediaSession.Token> getActiveSessionsForPackage (String packageName, ComponentName notificationListener)
Get a list of 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 Context.getPackageName(), this requires the 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 NotificationListenerService APIs, in which case you must pass the ComponentName of
your enabled listener. If none of them applies, it will throw a SecurityException.
| Parameters | |
|---|---|
packageName |
String: Package name of the application for which tokens should be fetched.
This value cannot be null. |
notificationListener |
ComponentName: The enabled notification listener component. May be null. |
| Returns | |
|---|---|
List<MediaSession.Token> |
A list of controllers for active sessions of the calling application.
This value cannot be null. |
getMediaKeyEventSession
public MediaSession.Token getMediaKeyEventSession ()
Gets the media key event session, which would receive a media key event unless specified.
This requires the Manifest.permission.MEDIA_CONTENT_CONTROL
permission be held by the calling app, or the app has an enabled notification listener
using the NotificationListenerService APIs. If none of them applies, it will throw
a SecurityException.
| Returns | |
|---|---|
MediaSession.Token |
The media key event session, which would receive key events by default, unless
the caller has specified the target. Can be null. |
getMediaKeyEventSessionPackageName
public String getMediaKeyEventSessionPackageName ()
Gets the package name of the media key event session.
This requires the Manifest.permission.MEDIA_CONTENT_CONTROL
permission be held by the calling app, or the app has an enabled notification listener
using the NotificationListenerService APIs. If none of them applies, it will throw
a SecurityException.
| Returns | |
|---|---|
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. |
See also:
getSession2Tokens
public List<Session2Token> getSession2Tokens ()
This method is deprecated.
MediaSession2 is deprecated.
Gets a list of Session2Token with type 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 MediaSession2.
This API is not generally intended for third party application developers. Apps wanting media session functionality should use the AndroidX Media3 Session Library.
| Returns | |
|---|---|
List<Session2Token> |
A list of Session2Token.
This value cannot be null. |
isTrustedForMediaControl
public boolean isTrustedForMediaControl (MediaSessionManager.RemoteUserInfo userInfo)
Checks whether the remote user is a trusted app.
An app is trusted if the app holds the
Manifest.permission.MEDIA_CONTENT_CONTROL permission or has an enabled
notification listener.
| Parameters | |
|---|---|
userInfo |
MediaSessionManager.RemoteUserInfo: The remote user info from either
MediaSession.getCurrentControllerInfo() or
MediaBrowserService.getCurrentBrowserInfo().
This value cannot be null. |
| Returns | |
|---|---|
boolean |
true if the remote user is trusted and its package name matches with the UID.
false otherwise. |
notifySession2Created
public void notifySession2Created (Session2Token token)
This method was deprecated
in API level 31.
Don't use this method. A new media session is notified automatically.
Notifies that a new MediaSession2 with type Session2Token.TYPE_SESSION is
created.
Do not use this API directly, but create a new instance through the
MediaSession2.Builder instead.
| Parameters | |
|---|---|
token |
Session2Token: newly created session2 token.
This value cannot be null. |
removeOnActiveSessionsChangedListener
public void removeOnActiveSessionsChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
Stop receiving active sessions updates on the specified listener.
| Parameters | |
|---|---|
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to remove.
This value cannot be null. |
removeOnActiveSessionsForPackageChangedListener
public void removeOnActiveSessionsForPackageChangedListener (MediaSessionManager.OnActiveSessionsChangedListener sessionListener)
Stop receiving active sessions updates on the specified listener for package.
| Parameters | |
|---|---|
sessionListener |
MediaSessionManager.OnActiveSessionsChangedListener: The listener to remove.
This value cannot be null. |
removeOnMediaKeyEventSessionChangedListener
public void removeOnMediaKeyEventSessionChangedListener (MediaSessionManager.OnMediaKeyEventSessionChangedListener listener)
Stop receiving updates on media key event session change on the specified listener.
| Parameters | |
|---|---|
listener |
MediaSessionManager.OnMediaKeyEventSessionChangedListener: A OnMediaKeyEventSessionChangedListener.
This value cannot be null. |
removeOnSession2TokensChangedListener
public void removeOnSession2TokensChangedListener (MediaSessionManager.OnSession2TokensChangedListener listener)
This method is deprecated.
MediaSession2 is deprecated.
Removes the 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.
| Parameters | |
|---|---|
listener |
MediaSessionManager.OnSession2TokensChangedListener: The listener to remove.
This value cannot be null. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-02-26 UTC.