自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
将深层链接集成到媒体应用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档旨在介绍第三方 (3P) 应用开发者如何为 AAOS 媒体应用添加深层链接。借助媒体深层链接,您可以通过深层链接打开 AAOS 媒体应用,就像在移动设备上打开一样。
支持的版本
如需获取最新的受支持版本,请参阅最新的构建制品。
CarMediaApp.apk
TestMediaApp.apk
运作方式
在 AAOS 上打开深层链接时,系统会打开处理特定架构的媒体应用。然后,媒体应用会对网址进行解码,使用链接中的信息创建 Media intent,然后使用该 intent 打开媒体界面。
支持新 intent 和新 extra 的实现:
第三方开发者负责更新其应用,以便应用可以通过网页 URI intent 调用,并通过 intent 向 Media 发送必要信息。
第三方应用需要满足的要求
以 TestMediaApp
应用中的 TmaTrampolineActivity
为例。
第 1 步
开发者必须在 TestMediaApp
中添加类似于 TmaTrampolineActivity
的 activity。如需创建深层链接,此 activity 需要在清单中添加 intent 过滤器。此 intent 过滤器应包含要处理的所有网址,如为传入链接添加 intent 过滤器中所述。
我们建议 intent 过滤器包含手机版应用使用的架构和主机,以确保深层链接可在各个平台上正常运作。
<activity android:name=".automotive.TmaTrampolineActivity"
android:exported="true">
<intent-filter android:label="TmaTrampolineActivity_label">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="app"
android:host="com.android.car.media.testmediaapp"/>
</intent-filter>
</activity>
第 2 步
在此 activity 中,Media 应用会从链接中检索信息,并为 Media 创建 intent。
Media 支持两个函数。第一个函数用于打开包含特定媒体内容的媒体内容,第二个函数用于显示搜索查询的结果。媒体内容 ID 或搜索查询字符串必须包含在 intent extra 中。
如需使用此功能,开发者必须安装最新版本的 Media,以支持 intent 操作 ACTION_MEDIA_TEMPLATE_V2
。该 intent 操作以及以下 intent extra 包含在 MediaIntentExtras.java
类中。这些 extra 可以添加到 intent 中。
extra 名称 |
值 |
说明 |
EXTRA_KEY_MEDIA_COMPONENT |
componentName 的字符串 |
此键用作 ACTION_MEDIA_TEMPLATE_V2 的字符串 extra 字段,用于指定用户希望通过哪个 MediaBrowserService 启动媒体。如果未指定,系统会打开处于启用状态的媒体来源。 |
EXTRA_KEY_MEDIA_ID |
媒体 ID |
此键用作 ACTION_MEDIA_TEMPLATE_V2 的字符串 extra 字段,用于指定应在浏览视图中显示的媒体文件。必须与 MediaBrowserServiceCompat API 中使用的 ID 一致。 |
EXTRA_KEY_SEARCH_QUERY |
搜索查询 |
此键用作 ACTION_MEDIA_TEMPLATE_V2 的字符串 extra 字段,用于指定要发送到当前 MediaBrowserService 或使用 EXTRA_KEY_MEDIA_COMPONENT 指定的 MediaBrowserService 的搜索查询 |
EXTRA_KEY_SEARCH_ACTION |
整数:
- 0:EXTRA_VALUE_NO_SEARCH_ACTION
- 1:EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH
|
此键用作 ACTION_MEDIA_TEMPLATE_V2 的整数 extra 字段,用于在加载搜索查询后指定 Media 要执行的操作。
值为 EXTRA_VALUE_NO_SEARCH_ACTION 或 EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH。此 extra 应仅与 EXTRA_KEY_SEARCH_QUERY 搭配使用。
如果未指定此 extra,则在搜索结果加载后不会执行进一步的操作。
|
特殊情况
如果将多个 extra 一起添加到 intent 中,例如此 intent 中同时包含 EXTRA_KEY_MEDIA_ID
和 EXTRA_KEY_SEARCH_QUERY
,则当前实现会先处理媒体 ID,并且只有在媒体 ID 为空时,Media 才会运行搜索查询。
测试
完成第三方媒体应用集成后,使用 adb
命令发送深层链接。Media 会打开并显示所需信息。例如,针对 TestMediaApp
的以下命令:
adb shell am start -W -a android.intent.action.VIEW -d "https://www.testmediaapp.com/path?search=normal\&searchAction=1"
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","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-03-26。"],[],[],null,["# Integrate deep links to Media apps\n\nThis document aims to describe how third party (3P) app developers can add deep\nlinks to AAOS media apps. Media deep links enable you to open AAOS Media apps\nthrough deep links in the same way as on a mobile device.\n\nSupported versions\n------------------\n\nTo get the latest supported version, see the latest\n[Build Artifacts](https://ci.android.com/builds/branches/aosp-ub-automotive-master/grid?legacy=1).\n\n- `CarMediaApp.apk`\n- `TestMediaApp.apk`\n\n### How it works\n\nWhen a deep link is opened on AAOS, it opens the media app that handles the\nparticular scheme. The media app then decodes the URL, creating a Media intent\nwith the information in that link and then using that intent to open the media\nscreen.\n\nThe implementation to support new intents and new extras:\n\n- Provides an updated format of the intents that can be handled by the Media\n to 3P developers so they can send necessary information through the intent\n to the Media.\n\n- Handles the intents from 3P apps that contain a specific media item or a\n search query and then open a page with the requested information in Media.\n\n3P developers are responsible for updating their app so that the app can be\ninvoked by the web URI intents and send the necessary information to the Media\nthrough an intent.\n\nRequirement for 3P apps\n-----------------------\n\nThe `TmaTrampolineActivity` in the `TestMediaApp` app is provided as an example.\n\n### Step 1\n\nDevelopers must have an activity similar to `TmaTrampolineActivity` in\n`TestMediaApp`. To deep link, this activity needs an intent filter in the\nmanifest. This intent filter should include all the URLs to be handled\nas described in [Add intent filters for incoming links](https://developer.android.com/training/app-links/deep-linking#adding-filters).\n\nWe suggest that the intent filter include the schemes and hosts that the\nphone version app uses to make sure the deep links work across platforms. \n\n \u003cactivity android:name=\".automotive.TmaTrampolineActivity\"\n android:exported=\"true\"\u003e\n \u003cintent-filter android:label=\"TmaTrampolineActivity_label\"\u003e\n \u003caction android:name=\"android.intent.action.VIEW\" /\u003e\n \u003ccategory android:name=\"android.intent.category.DEFAULT\" /\u003e\n \u003ccategory android:name=\"android.intent.category.BROWSABLE\" /\u003e\n \u003cdata android:scheme=\"app\"\n android:host=\"com.android.car.media.testmediaapp\"/\u003e\n \u003c/intent-filter\u003e\n \u003c/activity\u003e\n\n### Step 2\n\nIn this activity, the Media app retrieves the information from the link and\ncreates an intent to the Media.\n\nThe Media supports two functions. The first function is to open a\nmedia item with a specific media item and the second is to show the\nresults of a search query. The media item ID or the search query string must\nbe included in the intent extra.\n\n#### Intent information\n\nTo use this feature, developers must install the most recent version of the\nMedia to support the Intent action, `ACTION_MEDIA_TEMPLATE_V2`. The Intent\naction, and the following intent extras, are included in the\n`MediaIntentExtras.java` class. These extras can be added to the intent.\n\n| Extra name | Value | Description |\n|---------------------------|-----------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| EXTRA_KEY_MEDIA_COMPONENT | String for componentName | Key used as a string extra field with ACTION_MEDIA_TEMPLATE_V2 to specify the MediaBrowserService on which the user wants to start the media. When unspecified, the active media source is opened. |\n| EXTRA_KEY_MEDIA_ID | Media ID | Key used as a string extra field with ACTION_MEDIA_TEMPLATE_V2 specify the media item that should be displayed in the **Browse** view. Must match the IDs used in the MediaBrowserServiceCompat API. |\n| EXTRA_KEY_SEARCH_QUERY | Search query | Key used as a string extra field with ACTION_MEDIA_TEMPLATE_V2 to specify the search query to send either to the current MediaBrowserService or the one specified with EXTRA_KEY_MEDIA_COMPONENT |\n| EXTRA_KEY_SEARCH_ACTION | Integer: - 0: EXTRA_VALUE_NO_SEARCH_ACTION - 1: EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH | Key used as an int extra field with ACTION_MEDIA_TEMPLATE_V2 to specify the action for the Media to perform after the search query is loaded. The value is one of EXTRA_VALUE_NO_SEARCH_ACTION or EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH. This extra should only be used with EXTRA_KEY_SEARCH_QUERY. If this extra is not specified, then no further action is taken after the search results are loaded. |\n\n#### Special cases\n\nIf several extras are added to the intent together, such as both\n`EXTRA_KEY_MEDIA_ID` and `EXTRA_KEY_SEARCH_QUERY` are included in this intent,\nthe current implementation handles the media ID first, and only when the media\nID is empty does the Media run the search query.\n\nTest\n----\n\nOnce integration of the 3P media app is complete, use an `adb` command to send\na deep link. Media opens up with the desired information. For example, this\ncommand for `TestMediaApp`: \n\n```\nadb shell am start -W -a android.intent.action.VIEW -d \"https://www.testmediaapp.com/path?search=normal\\&searchAction=1\"\n```"]]