自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
用媒体实现电台功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
电台界面以独立应用的形式实现。如需了解有关如何将电台界面集成到电台硬件的说明,请参阅实现电台。
以下部分介绍了如何将电台界面与媒体集成,以便为用户提供无缝体验,让用户能够与媒体来源和电台互动,就像它们是单个应用一样。
下图说明了电台和媒体的参考实现如何实现应用切换用户流。

图 1. 媒体来源切换用户流
为了在电台与媒体中的其他应用之间流畅切换,car-media-common
库定义了可用于启动媒体来源选择器的 Android intent。在 AOSP 中,此选择器在应用启动器中实现,为启动应用呈现相同的界面,但经过滤后仅显示媒体来源。
OEM 可以按原样采用当前的应用启动器实现,也可以实现自定义的媒体来源选择器。
此选择器可以在两种模式下工作:
- 正常流程 - 使用此选择器后,所选来源会显示在媒体中,以便用户浏览其中内容。
- 切换模式 - 使用选择器切换来源,但系统不会向用户显示媒体内容。首页上的“选择器”图标也是如此。选择来源后,系统会向用户显示最近显示的上一个屏幕(在本例中为首页)。
用于在媒体来源之间切换的 intent 可以通过 MediaSource#getSourceSelectorIntent()
方法获取,该方法接受返回 intent 以启动上述每个流程的 popup
布尔值。
packages/apps/Car/libs/car-media-common/res/values/config.xml
中定义了实际 intent。如要自定义此配置,请使用构建时叠加层。
替换电台应用
鉴于电台应用实现了媒体浏览和媒体会话,因此应用启动器中显示了电台。为了防止在用户点击图标时启动媒体,需要满足以下两个元素。电台必须:
- 具有启动器 activity。
- 声明为自定义来源。为此,请将组件名称添加到
car-media-common/res/values/config.xml
中的 custom_media_packages
键。
用户体验驾驶限制条件
媒体必须遵守所有防止驾驶员分心的用户体验限制条件。为此,媒体必须监听 CarUXRestrictionManager 并实现其所有政策。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","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-07-27。"],[],[],null,["# Implement radio with Media\n\nThe Radio UI is implemented as an independent app. Instructions on how to integrate a\nRadio UI to the radio hardware can be found at\n[Implement radio](/docs/automotive/broadcast-radio).\n\nThe following section describes how to integrate Radio UI with Media to provide users with a\nseamless experience that enables users to interact with media sources and radio as if they were a\nsingle app.\n\nMedia source switching user flow\n--------------------------------\n\nThe following diagram illustrates how the reference implementation of Radio and Media implements\nthe app switching user flow.\n\n**Figure 1.** Media source switching user flow\n\nTo provide a seamless transition between Radio and other apps in Media, the\n`car-media-common` library defines Android intents that can be used to launch a media\nsource selector. In AOSP, this selector is implemented in the App Launcher, presenting the same UI\nfor launching apps but filtered to display only media sources.\n\nOEMs can opt to take the current App Launcher implementation as is, or implement a customized\nmedia source selector.\n\nThe selector can act in two modes:\n\n- **Normal flow.** After using the selector, the selected source is displayed in Media so the user can browse its content.\n- **As a switch.** A selector is used to switch sources, but the media is not displayed to the user. This is true of the Selector icon on the Home Page. After selecting a source, the most recent previous screen is displayed to the user (in this case, the Home page).\n\nThe intent used to switch between media sources can be obtained from the\n`MediaSource#getSourceSelectorIntent()` method, which accepts a `popup`\nBoolean that returns an intent to launch each of the flows described above.\n\nThe actual intents are defined at\n`packages/apps/Car/libs/car-media-common/res/values/config.xml`. To\ncustomize this configuration, use build-time overlays.\n\n### Replace the Radio app\n\nGiven that the Radio app implements Media Browse and Media Session, Radio is displayed\nin the App launcher. To prevent launching Media when a user clicks the icon, two elements\nare required. Radio *must*:\n\n- Have a *launcher* activity.\n- Be declared as a *custom source* . To do so, add the component name to the `custom_media_packages` key in `car-media-common/res/values/config.xml`.\n\nUX driving restrictions\n-----------------------\n\nMedia must observe all UX driving distraction restrictions. To do so, Media must listen to the\n[CarUXRestrictionManager](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictionsManager)\nand implement all its policies.\n\n- Media must connect to the [Car](https://developer.android.com/reference/android/car/Car) library and get an instance of [CarUXRestrictionManager](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictionsManager).\n- Media must subscribe to updates in the list of [CarUxRestrictions](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions) and implement them as documented.\n- Particularly important for Media are:\n - [UX_RESTRICTIONS_NO_SETUP](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions.html#UX_RESTRICTIONS_NO_SETUP). In this case, the sign-in flow must be disabled.\n - [UX_RESTRICTIONS_LIMIT_STRING_LENGTH](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions.html#UX_RESTRICTIONS_LIMIT_STRING_LENGTH). Error messages and other text provided by the media apps must be limited to the given length."]]