自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
小工具和捷徑
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android 8.0 中新增的流程 API 可讓應用程式開發人員在應用程式中新增捷徑和小工具,而非依賴小工具匣。基於安全考量,這項功能也已淘汰舊有的新增捷徑方法 (傳送廣播)。
啟動器必須支援這項新功能,讓應用程式開發人員可以依靠系統新增捷徑或小工具。
範例和來源
文件可在各種系統類別中使用。參考資料包括:
- ShortcutManager.java
在標頭中發布已固定的捷徑區段。 - Intent.java
ACTION_CREATE_SHORTCUT 的 Javadoc。
- AppWidgetManager.java
requestPinAppWidget 的 Javadoc。
實作
裝置實作人員需要在啟動器中加入意圖篩選器動作的活動:
android.content.pm.action.CONFIRM_PIN_SHORTCUT
android.content.pm.action.CONFIRM_PIN_APPWIDGET
詳情請參閱 API 說明文件。
這個活動應向使用者顯示確認提示,要求他們新增所要求的小工具/捷徑,並在接受後,在主畫面上新增對應的小工具/捷徑。
如果是小工具,accept()
呼叫應包含新加入的小工具 ID。
實作系統 UI
合作夥伴需要參考 Launcher3 導入方式 (packages/apps/Launcher3
) 更新啟動器應用程式。
在 Android 開放原始碼計畫 (AOSP) 中找出相關的 Launcher3 變更:
驗證
如要驗證這項功能,請嘗試透過 Chrome 或聯絡人應用程式新增捷徑,並確認是否會顯示適當的確認提示。接受後,圖示應會新增至主畫面,Chrome 也應會顯示成功的快訊。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Widgets and shortcuts\n\nThe new flow API for adding shortcuts and widgets in Android 8.0 allows\napp developers to add shortcuts and widgets from inside the app instead\nof relying on the widget tray. It also deprecates the old method (sending a\nbroadcast) of adding shortcuts for security reasons.\n\n\nLaunchers need to support this new implementation so that app developers can\nrely on the system to add their shortcut or widget.\n\nExamples and source\n-------------------\n\n\nDocs are available in various system class. References include the following:\n\n- **[ShortcutManager.java](https://developer.android.com/reference/android/content/pm/ShortcutManager.html)** \n Publish Pinned shortcut section in header.\n- **Intent.java** \n Javadoc for ACTION_CREATE_SHORTCUT.\n- **AppWidgetManager.java** \n Javadoc for requestPinAppWidget.\n\nImplementation\n--------------\n\n\nDevice implementers need to add an activity in their launcher with intent filter actions:\n\n- `android.content.pm.action.CONFIRM_PIN_SHORTCUT`\n- `android.content.pm.action.CONFIRM_PIN_APPWIDGET`\n\n\nRefer to the [API\ndocumentation](https://developer.android.com/reference/android/content/pm/LauncherApps.html) for more details.\n\n\nThis activity should display a confirmation prompt to the user to add the [requested\nwidget/shortcut](https://developer.android.com/reference/android/content/pm/LauncherApps.html) and upon accepting, add the corresponding widget/shortcut on\nthe homescreen.\n\n\nIn case of widgets, the `accept()` call should include the widget ID\nof the newly added widget.\n\n### Implementing a System UI\n\n\nPartners need to update their Launcher app using the Launcher3 implementation\n(`packages/apps/Launcher3`) as reference.\n\n\nFind the relevant Launcher3 changes in the Android Open Source Project (AOSP):\n\n- Change-Id: [8b2002e28072c52d78f6d052c6ff6da50a2d0324](https://android.googlesource.com/platform/packages/apps/Launcher3/+/8b2002e28072c52d78f6d052c6ff6da50a2d0324)\n- Change-Id: [278359539c02ca160caf1df54ce96053a2a2ef59](https://android.googlesource.com/platform/packages/apps/Launcher3/+/278359539c02ca160caf1df54ce96053a2a2ef59)\n\nValidation\n----------\n\n\nTo validate the feature, try to add shortcuts from Chrome or the Contacts app\nand verify that a proper confirmation prompt is shown. Upon accepting, the icon\nshould get added on the homescreen and Chrome should display a success toast."]]