Search code examples
androidmediastoreandroid-auto

I am developing Android Auto Media App and not able to understand life cycle of MediaBrowserService


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.


Solution

  • 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).