媒體瀏覽器服務生命週期
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本節說明 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-22 (世界標準時間)。
[null,null,["上次更新時間: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)."]]