Android APIs
public abstract class

MediaBrowserService

extends Service
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.app.Service
         ↳ android.service.media.MediaBrowserService

Class Overview

Base class for media browse services.

Media browse services enable applications to browse media content provided by an application and ask the application to start playing it. They may also be used to control content that is already playing by way of a MediaSession.

To extend this class, you must declare the service in your manifest file with an intent filter with the SERVICE_INTERFACE action. For example:

 <service android:name=".MyMediaBrowserService"
          android:label="@string/service_name" >
     <intent-filter>
         <action android:name="android.media.browse.MediaBrowserService" />
     </intent-filter>
 </service>
 

Summary

Nested Classes
class MediaBrowserService.BrowserRoot Contains information that the browser service needs to send to the client when first connected. 
class MediaBrowserService.Result<T> Completion handler for asynchronous callback methods in MediaBrowserService
Constants
String SERVICE_INTERFACE The Intent that must be declared as handled by the service.
[Expand]
Inherited Constants
From class android.app.Service
From class android.content.Context
From interface android.content.ComponentCallbacks2
Public Constructors
MediaBrowserService()
Public Methods
void dump(FileDescriptor fd, PrintWriter writer, String[] args)
Print the Service's state into the given stream.
MediaSession.Token getSessionToken()
Gets the session token, or null if it has not yet been created or if it has been destroyed.
void notifyChildrenChanged(String parentId)
Notifies all connected media browsers that the children of the specified parent id have changed in some way.
IBinder onBind(Intent intent)
Return the communication channel to the service.
void onCreate()
Called by the system when the service is first created.
abstract MediaBrowserService.BrowserRoot onGetRoot(String clientPackageName, int clientUid, Bundle rootHints)
Called to get the root information for browsing by a particular client.
abstract void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result)
Called to get information about the children of a media item.
void setSessionToken(MediaSession.Token token)
Call to set the media session.
[Expand]
Inherited Methods
From class android.app.Service
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object
From interface android.content.ComponentCallbacks2
From interface android.content.ComponentCallbacks

Constants

public static final String SERVICE_INTERFACE

Added in API level 21

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

Constant Value: "android.media.browse.MediaBrowserService"

Public Constructors

public MediaBrowserService ()

Added in API level 21

Public Methods

public void dump (FileDescriptor fd, PrintWriter writer, String[] args)

Added in API level 21

Print the Service's state into the given stream. This gets invoked if you run "adb shell dumpsys activity service <yourservicename>" (note that for this command to work, the service must be running, and you must specify a fully-qualified service name). This is distinct from "dumpsys <servicename>", which only works for named system services and which invokes the dump(FileDescriptor, String[]) method on the IBinder interface registered with ServiceManager.

Parameters
fd The raw file descriptor that the dump is being sent to.
writer The PrintWriter to which you should dump your state. This will be closed for you after you return.
args additional arguments to the dump request.

public MediaSession.Token getSessionToken ()

Added in API level 21

Gets the session token, or null if it has not yet been created or if it has been destroyed.

public void notifyChildrenChanged (String parentId)

Added in API level 21

Notifies all connected media browsers that the children of the specified parent id have changed in some way. This will cause browsers to fetch subscribed content again.

Parameters
parentId The id of the parent media item whose children changed.

public IBinder onBind (Intent intent)

Added in API level 21

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.

Parameters
intent The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Returns
  • Return an IBinder through which clients can call on to the service.

public void onCreate ()

Added in API level 21

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

public abstract MediaBrowserService.BrowserRoot onGetRoot (String clientPackageName, int clientUid, Bundle rootHints)

Added in API level 21

Called to get the root information for browsing by a particular client.

The implementation should verify that the client package has permission to access browse media information before returning the root id; it should return null if the client is not allowed to access this information.

Parameters
clientPackageName The package name of the application which is requesting access to browse media.
clientUid The uid of the application which is requesting access to browse media.
rootHints An optional bundle of service-specific arguments to send to the media browse service when connecting and retrieving the root id for browsing, or null if none. The contents of this bundle may affect the information returned when browsing.

public abstract void onLoadChildren (String parentId, Result<List<MediaBrowser.MediaItem>> result)

Added in API level 21

Called to get information about the children of a media item.

Implementations must call result.result.sendResult with the list of children. If loading the children will be an expensive operation that should be performed on another thread, result.result.detach may be called before returning from this function, and then result.sendResult called when the loading is complete.

Parameters
parentId The id of the parent media item whose children are to be queried.
Returns
  • The list of children, or null if the id is invalid.

public void setSessionToken (MediaSession.Token token)

Added in API level 21

Call to set the media session.

This should be called as soon as possible during the service's startup. It may only be called once.