自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
將深層連結整合至媒體應用程式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文旨在說明第三方 (3P) 應用程式開發人員如何為 AAOS 媒體應用程式新增深層連結。媒體深層連結可讓您透過深層連結開啟 AAOS Media 應用程式,就像在行動裝置上開啟應用程式一樣。
支援的版本
如要取得最新的支援版本,請參閱最新的建構構件。
CarMediaApp.apk
TestMediaApp.apk
運作方式
在 AAOS 上開啟深層連結時,系統會開啟處理特定配置的媒體應用程式。媒體應用程式接著解碼網址,使用該連結中的資訊建立媒體意圖,然後使用該意圖開啟媒體畫面。
支援新意圖和新額外項目的實作方式:
第三方開發人員有責任更新應用程式,讓應用程式能夠透過網頁 URI 意圖叫用,並透過意圖將必要資訊傳送至媒體。
第三方應用程式相關規定
TestMediaApp
應用程式中的 TmaTrampolineActivity
為範例。
步驟 1
開發人員必須擁有類似 TmaTrampolineActivity
的活動,才能在 TestMediaApp
中執行。如要建立深層連結,此活動需要在資訊清單中設定意圖篩選器。這個意圖篩選器應包含所有要處理的網址,如「新增意圖篩選器以處理傳入連結」一節所述。
建議您在意圖篩選器中加入手機版應用程式使用的配置和主機,確保深層連結可跨平台運作。
<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
在這個活動中,Media 應用程式會從連結擷取資訊,並建立對 Media 的意圖。
Media 支援兩種功能。第一個函式可開啟包含特定媒體項目的媒體項目,第二個函式則可顯示搜尋查詢的結果。意圖額外資料中必須包含媒體項目 ID 或搜尋查詢字串。
如要使用這項功能,開發人員必須安裝最新版的 Media,以支援 Intent 動作 ACTION_MEDIA_TEMPLATE_V2
。意圖動作和下列意圖額外項目已包含在 MediaIntentExtras.java
類別中。這些額外資料可新增至意圖。
額外名稱 |
值 |
說明 |
EXTRA_KEY_MEDIA_COMPONENT |
用於 componentName 的字串 |
用於指定使用者要啟動媒體的 MediaBrowserService 時,以字串額外欄位形式使用此鍵。如果未指定,系統會開啟目前的媒體來源。 |
EXTRA_KEY_MEDIA_ID |
媒體 ID |
此鍵會做為字串額外欄位使用,並搭配 ACTION_MEDIA_TEMPLATE_V2 指定應在「瀏覽」檢視畫面中顯示的媒體項目。必須與 MediaBrowserServiceCompat API 中使用的 ID 相符。 |
EXTRA_KEY_SEARCH_QUERY |
搜尋查詢 |
用於指定要傳送至目前 MediaBrowserService 或使用 EXTRA_KEY_MEDIA_COMPONENT 指定的 MediaBrowserService 的搜尋查詢的字串額外欄位 |
EXTRA_KEY_SEARCH_ACTION |
整數:
- 0:EXTRA_VALUE_NO_SEARCH_ACTION
- 1: EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH
|
此鍵值會做為 int 額外欄位使用,並搭配 ACTION_MEDIA_TEMPLATE_V2,指定媒體在搜尋查詢載入後要執行的動作。 值為 EXTRA_VALUE_NO_SEARCH_ACTION 或 EXTRA_VALUE_PLAY_FIRST_ITEM_FROM_SEARCH。這個額外項目應只與 EXTRA_KEY_SEARCH_QUERY 搭配使用。
如果未指定這個額外項目,系統就不會在載入搜尋結果後採取進一步行動。
|
特殊情況
如果有幾個額外項目一起新增至意圖,例如意圖中同時包含 EXTRA_KEY_MEDIA_ID
和 EXTRA_KEY_SEARCH_QUERY
,目前的實作會先處理媒體 ID,只有在媒體 ID 為空白時,Media 才會執行搜尋查詢。
測試
第三方媒體應用程式整合完成後,請使用 adb
指令傳送深層連結。媒體會開啟並顯示所需資訊。例如,以下是 TestMediaApp
的命令:
adb shell am start -W -a android.intent.action.VIEW -d "https://www.testmediaapp.com/path?search=normal\&searchAction=1"
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-06-12 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-06-12 (世界標準時間)。"],[],[],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```"]]