# Print output for @column tags ?> MediaSession2Service - Android SDK | Android Developers

Most visited

Recently visited

MediaSession2Service

public abstract class MediaSession2Service
extends Service

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.media.MediaSession2Service


This API is not generally intended for third party application developers. Use the AndroidX Media2 session Library for consistent behavior across all devices.

Service containing MediaSession2.

Summary

Nested classes

class MediaSession2Service.MediaNotification

This API is not generally intended for third party application developers. 

Constants

String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

Inherited constants

Public constructors

MediaSession2Service()

Public methods

final void addSession(MediaSession2 session)

Adds a session to this service.

final List<MediaSession2> getSessions()

Gets the list of MediaSession2s that you've added to this service.

IBinder onBind(Intent intent)

Return the communication channel to the service. If you override this method you must call through to the superclass implementation.

void onCreate()

Called by the system when the service is first created.

void onDestroy()

Called by the system to notify that it is no longer used and is being removed.

abstract MediaSession2 onGetSession(MediaSession2.ControllerInfo controllerInfo)

Called when a MediaController2 is created with the this service's Session2Token.

abstract MediaSession2Service.MediaNotification onUpdateNotification(MediaSession2 session)

Called when notification UI needs update.

final void removeSession(MediaSession2 session)

Removes a session from this service.

Inherited methods

Constants

SERVICE_INTERFACE

public static final String SERVICE_INTERFACE

The Intent that must be declared as handled by the service.

Constant Value: "android.media.MediaSession2Service"

Public constructors

MediaSession2Service

public MediaSession2Service ()

Public methods

addSession

public final void addSession (MediaSession2 session)

Adds a session to this service.

Added session will be removed automatically when it's closed, or removed when removeSession(MediaSession2) is called.

Parameters
session MediaSession2: a session to be added. This value cannot be null.

See also:

getSessions

public final List<MediaSession2> getSessions ()

Gets the list of MediaSession2s that you've added to this service.

Returns
List<MediaSession2> sessions This value cannot be null.

onBind

public IBinder onBind (Intent intent)

Return the communication channel to the service. May return null if clients can not bind to the service. The returned IBinder is usually for a complex interface that has been described using aidl.

Note that unlike other application components, calls on to the IBinder interface returned here may not happen on the main thread of the process. More information about the main thread can be found in Processes and Threads.

If you override this method you must call through to the superclass implementation.

Parameters
intent Intent: This value cannot be null.

Returns
IBinder This value may be null.

onCreate

public void onCreate ()

Called by the system when the service is first created. Do not call this method directly.

Override this method if you need your own initialization. Derived classes MUST call through to the super class's implementation of this method.
If you override this method you must call through to the superclass implementation.

onDestroy

public void onDestroy ()

Called by the system to notify that it is no longer used and is being removed. Do not call this method directly.

Override this method if you need your own clean up. Derived classes MUST call through to the super class's implementation of this method.
If you override this method you must call through to the superclass implementation.

onGetSession

public abstract MediaSession2 onGetSession (MediaSession2.ControllerInfo controllerInfo)

Called when a MediaController2 is created with the this service's Session2Token. Return the session for telling the controller which session to connect. Return null to reject the connection from this controller.

Session returned here will be added to this service automatically. You don't need to call addSession(android.media.MediaSession2) for that.

This method is always called on the main thread.

Parameters
controllerInfo MediaSession2.ControllerInfo: information of the controller which is trying to connect. This value cannot be null.

Returns
MediaSession2 a MediaSession2 instance for the controller to connect to, or null to reject connection

See also:

onUpdateNotification

public abstract MediaSession2Service.MediaNotification onUpdateNotification (MediaSession2 session)

Called when notification UI needs update. Override this method to show or cancel your own notification UI.

This would be called on MediaSession2's callback executor when playback state is changed.

With the notification returned here, the service becomes foreground service when the playback is started. Apps must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API. It becomes background service after the playback is stopped.

Parameters
session MediaSession2: a session that needs notification update. This value cannot be null.

Returns
MediaSession2Service.MediaNotification a MediaNotification. Can be null.

removeSession

public final void removeSession (MediaSession2 session)

Removes a session from this service.

Parameters
session MediaSession2: a session to be removed. This value cannot be null.

See also: