1

While creating android Auto media App MusicService class gets created extending MediaBrowserService which two methods gets auto implemented onGetRoot and onLoadChildren. Can any one give detail explanation how every thing works.

Rahul
  • 479
  • 3
  • 18

2 Answers2

3

Life cycle of MediaBrowserService looks like life cycle of simple Service which it extends. From documentation:

The lifecycle of the MediaBrowserService is controlled by the way it is created, the number of clients that have are to it, and the calls it receives from media session callbacks. To summarize:

  • The service is created when it is started in response to a media button or when an activity binds to it (after connecting via its MediaBrowser).
  • The media session onPlay() callback should include code that calls startService(). This ensures that the service starts and continues to run, even when all UI MediaBrowser activities that are bound to it unbind.
  • The onStop() callback should call stopSelf(). If the service was started, this stops it. In addition, the service is destroyed if there are no activities bound to it. Otherwise, the service remains bound until all its activities unbind. (If a subsequent startService() call is received before the service is destroyed, the pending stop is cancelled.)

The following flowchart demonstrates how the lifecycle of a service is managed. The variable counter tracks the number of bound clients: life cycle

onGetRoot and onLoadChildren used to manage client connections.

But to use MediaBrowserService this is not enough, so you should read this documentation (follow nested links).

B-GangsteR
  • 2,534
  • 22
  • 34
2

Taken from MusicPlayer.java:

This class provides a MediaBrowser through a service. It exposes the media library to a browsing client, through the onGetRoot and onLoadChildren methods

onGetRoot(..): Returns the root id if the client package has permission to access media information (Returns null if the client is not allowed). Source

onLoadChildren: Returns a list of children of a media item. Source

To get a idea of the implementation of both check the implementation in MusicPlayer.java

IIIIIIIIIIIIIIIIIIIIII
  • 3,958
  • 5
  • 45
  • 70