媒体浏览器服务生命周期
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分介绍在典型的用户工作流中,Android Automotive OS (AAOS) 和 Android Auto 如何与您的媒体浏览器服务互动:
用户在 AAOS 或 Android Auto 中启动您的应用。
宿主应用会绑定到您的媒体浏览器服务,如果该服务尚未运行,操作系统会启动它。在 onCreate()
方法的实现中,您必须创建并注册 MediaSessionCompat
对象及其回调对象。如需了解详情,请参阅注册媒体会话。
AAOS 或 Android Auto 调用您的服务的 onGetRoot
方法,以检索内容层次结构中的根。未显示根。
而是会在后续步骤中从您的应用中检索更多内容。
AAOS 或 Android Auto 调用服务的 onLoadChildren()
方法来检索根媒体项的后代。AAOS 和 Android Auto 会将这些媒体项显示为顶级内容项。如需详细了解系统期望的内容,请参阅构造根菜单。
当用户选择可浏览的媒体项时,系统会再次调用服务的 onLoadChildren()
方法。
如果用户选择了可播放媒体项,AAOS 或 Android Auto 会调用相应的媒体会话回调方法来执行该操作。
如果您的应用支持,用户还可以搜索您的内容。在这种情况下,AAOS 或 Android Auto 会调用服务的 onSearch()
方法。
如需了解详情,请参阅显示可浏览的搜索结果。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-22。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-22。"],[],[],null,["# Media browser service lifecycle\n\nThis section describes how Android Automotive OS (AAOS) and Android Auto\ninteract with your media browser service during a typical user workflow:\n\n1. The user launches your app on AAOS or Android Auto.\n\n2. The host app binds to your media browser service, which the OS starts if it\n isn't already running. In your implementation of the `onCreate()` method,\n you must create and register a [`MediaSessionCompat`](/reference/android/support/v4/media/session/MediaSessionCompat) object and its\n callback object. To learn more, see [Register a media session](/training/cars/media/enable-playback#registering_mediasession).\n\n3. AAOS or Android Auto calls your service's [`onGetRoot`](/training/cars/media/content-hierarchy#onGetRoot) method to\n retrieve the root in your content hierarchy. The root is not displayed.\n Instead, it retrieves more content from your app in subsequent steps.\n\n4. AAOS or Android Auto calls your service's [`onLoadChildren()`](/reference/androidx/media/MediaBrowserServiceCompat#onLoadChildren(java.lang.String,androidx.media.MediaBrowserServiceCompat.Result%3Cjava.util.List%3Candroid.support.v4.media.MediaBrowserCompat.MediaItem%3E%3E)%5D) method to\n retrieve the descendants of the root media item. AAOS and Android Auto\n display these media items as the top level of content items. See\n [Structure the root menu](/training/cars/media/content-media-browser/content-hierarchy#root-menu-structure) to learn more about what the system expects.\n\n5. When the user selects a **browsable** media item, your service's\n `onLoadChildren()` method is called again.\n\n6. If the user selects a **playable** media item, AAOS or Android Auto calls\n the appropriate media session callback method to perform the action.\n\n7. If supported by your app, the user can also search your content. In this\n case, AAOS or Android Auto call your service's [`onSearch()`](/reference/androidx/media/MediaBrowserServiceCompat#onSearch(java.lang.String,android.os.Bundle,androidx.media.MediaBrowserServiceCompat.Result%3Cjava.util.List%3Candroid.support.v4.media.MediaBrowserCompat.MediaItem%3E%3E)) method.\n To learn more, see [Display browsable search results](/training/cars/media/create-media-browser/browsable-search)."]]