為捷徑新增功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以透過 shortcuts.xml
中的功能宣告幾種動作類型,讓使用者可在啟動應用程式後直接執行特定工作。
舉例來說,Google 助理應用程式動作就使用相關功能,讓開發人員將應用程式內功能延伸至內建意圖 (BII),因此使用者能夠使用口語指令來啟用及控制這些應用程式內功能。程式碼的功能包含動作名稱和 intent
,後者指向應用程式中能夠解決使用者意圖的目的地。
在 shortcuts.xml 中定義功能
請在 Android 應用程式開發專案的 shortcuts.xml
資源檔案中定義 capability
元素。如要定義 capability
元素,請按照下列步驟操作:
- 按照「建立靜態捷徑」中的操作說明建立
shortcuts.xml
資源。
請在功能中加入下列必要資訊:
功能名稱:您希望應用程式執行的動作。如要瞭解需要程式碼功能定義的功能,請參閱元件說明文件。應用程式動作的語音指令使用 BII Action ID
做為功能名稱,詳見 BII 參考資料。舉例來說,GET_THING
BII 會將 Action ID
列為 actions.intent.GET_THING
。
應用程式目的地:動作為了滿足使用者要求而啟動的應用程式內目的地。請使用以巢狀結構包含在 capability
內的 intent
元素來定義應用程式目的地。
參數對應:每個 intent
可能包含要以意圖的 extra
資料形式傳遞的參數。舉例來說,每個應用程式動作 BII 都有欄位代表著使用者在觸發 BII 的查詢中通常會提供的資訊。
下列範例示範 shortcuts.xml
中的 actions.intent.START_EXERCISE
功能定義,這個 BII 可讓使用者透過 Google 助理使用口語指令,開始健身應用程式中的訓練:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<capability android:name="actions.intent.START_EXERCISE">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.example.sampleApp"
android:targetClass="com.example.sampleApp.ExerciseActivity">
<parameter
android:name="exercise.name"
android:key="exerciseType"/>
</intent>
</capability>
</shortcuts>
在上述範例中,<capability>
android:name
屬性會參照 START_EXERCISE
BII。如果使用者問 Google 助理「Ok Google,在 ExampleApp 中開始跑步」,從而叫用這個 BII,Google 助理就會使用巢狀 intent
元素中提供的資訊來執行使用者的要求。此範例中的 intent
定義了下列詳細資料:
android:targetPackage
會設定此意圖的目標應用程式套件。
android:targetClass
欄位會指定目的地活動:com.example.sampleApp.ExerciseActivity
。
- 意圖
parameter
會宣告支援 BII 參數 exercise.name
,以及如何將收集自使用者的參數值,做為 intent
中的額外資料傳遞。
建立捷徑與功能的關聯
定義功能後,您可以建立功能與靜態或動態捷徑的關聯,藉此擴充其功能。捷徑連結至 capability
的方式取決於實作的功能,以及使用者要求中包含的實際字詞。舉例來說,假設使用者問 Google 助理「Ok Google,在 ExampleApp 中開始跑步」,然後在健身追蹤應用程式中開始跑步。Google 助理可以使用捷徑來啟動 capability
的執行個體,其為 exercise.name
參數定義了有效的運動實體「跑步」。
如要進一步瞭解如何建立捷徑與應用程式動作的關聯,請參閱「應用程式動作總覽」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[[["容易理解","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-08-21 (世界標準時間)。"],[],[],null,["# Add capabilities to shortcuts\n\nCapabilities in `shortcuts.xml` let you declare the types of actions users can\ntake to launch your app and directly perform a specific task.\n\nFor example, Google Assistant App Actions use capabilities to let developers\nextend in-app features to [built-in intents](//developers.google.com/assistant/app/intents) (BIIs), letting users activate\nand control those features using spoken commands. A capability consists of the\nname of the action and an `intent` targeting the destination in your app that\nresolves the user intent.\n\nDefine capabilities in shortcuts.xml\n------------------------------------\n\nYou define `capability` elements in a `shortcuts.xml` resource file in your\nAndroid app development project. To define a `capability` element, do the\nfollowing:\n\n1. Create a `shortcuts.xml` resource by following the instructions in [Create\n static shortcuts](//develop/ui/views/launch/shortcuts/creating-shortcuts#static).\n2. Include the following required information in your capability:\n\n - **Capability name:** the action you want your app to support. Refer to\n the component documentation for the feature that requires capability\n definitions. App Actions voice-enabled commands use the BII `Action ID`\n for capability names, which you can find in [BII reference](//developers.google.com/assistant/app/reference/built-in-intents). For example,\n the [`GET_THING`](//developers.google.com/assistant/app/reference/built-in-intents/common/get-thing) BII lists its `Action ID` as `actions.intent.GET_THING`.\n\n - **App destination:** the destination in your app the action launches to\n fulfill the user request. Define app destinations using `intent` elements\n nested within the `capability`.\n\n - **Parameter mappings:** each `intent` might contain parameters to be\n passed as `extra` data of the intent. For example, each App Actions BII\n includes fields representing information users often provide in queries that\n trigger the BII.\n\nThe following example demonstrates a capability definition in `shortcuts.xml`\nfor [`actions.intent.START_EXERCISE`](//developers.google.com/assistant/app/reference/built-in-intents/health-and-fitness/start-exercise), a BII that lets users use spoken\ncommands with Assistant to begin a workout in a fitness app: \n\n \u003cshortcuts xmlns:android=\"http://schemas.android.com/apk/res/android\"\u003e\n \u003ccapability android:name=\"actions.intent.START_EXERCISE\"\u003e\n \u003cintent\n android:action=\"android.intent.action.VIEW\"\n android:targetPackage=\"com.example.sampleApp\"\n android:targetClass=\"com.example.sampleApp.ExerciseActivity\"\u003e\n \u003cparameter\n android:name=\"exercise.name\"\n android:key=\"exerciseType\"/\u003e\n \u003c/intent\u003e\n \u003c/capability\u003e\n \u003c/shortcuts\u003e\n\nIn the preceding example, the `\u003ccapability\u003e` `android:name` attribute refers to\nthe `START_EXERCISE` BII. If a user invokes this BII by asking Assistant, *\"Hey\nGoogle, start a run in ExampleApp,\"* Assistant fulfills the user request using\ninformation provided in the nested `intent` element. The `intent` in this sample\ndefines the following details:\n\n- The `android:targetPackage` sets the target application package for this intent.\n- The `android:targetClass` field specifies the destination activity: `com.example.sampleApp.ExerciseActivity`.\n- The intent `parameter` declares support for a BII parameter [`exercise.name`](//developers.google.com/assistant/app/reference/built-in-intents/health-and-fitness/start-exercise#recommended-fields) and how to pass the parameter value---collected from the user---as extra data in the `intent`.\n\nAssociate shortcuts with a capability\n-------------------------------------\n\nOnce you define a capability, you can extend its functionality by associating\nstatic or dynamic shortcuts with it. How shortcuts are linked to a `capability`\ndepends on the feature being implemented and the actual words included in a\nuser's request. For example, when a user begins a run in your fitness tracking\napp by asking Assistant, *\"Hey Google, start a run in ExampleApp.\"* Assistant\ncan use a shortcut to launch an instance of a `capability` that defines a valid\nexercise entity of \"run\" for the `exercise.name` parameter.\n\nFor more information about associating shortcuts to App Actions, see [App\nActions overview](//developers.google.com/assistant/app)."]]