为快捷方式添加功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
shortcuts.xml
中的功能可让您声明用户启动应用时可以执行的操作类型,并直接执行特定任务。
例如,Google 助理的与应用有关的 Action 通过使用能力使开发者可将应用内功能扩展到内置 intent (BII),从而让用户能够使用语音指令激活和控制这些功能。功能包含操作的名称和 intent
(用于定位到解析用户 intent 的应用中的目的地)。
在 shortcuts.xml 中定义能力
您可以在 Android 应用开发项目的 shortcuts.xml
资源文件中定义 capability
元素。如需定义 capability
元素,请执行以下操作:
- 按照创建静态快捷方式中的说明创建
shortcuts.xml
资源。
在能力中添加以下必需信息:
功能名称:您希望应用支持的操作。如需了解需要能力定义的功能,请参阅组件文档。支持与应用有关的 Action 语音的指令会使用 BII Action ID
作为能力名称,您可以在 BII 参考文档中找到这些名称。例如,GET_THING
BII 会将其 Action ID
列为 actions.intent.GET_THING
。
应用目的地:应用中的目的地,操作启动该目的地来执行用户请求。使用嵌套在 capability
中的 intent
元素定义应用目的地。
参数映射:每个 intent
可能包含要作为 intent 的 extra
数据传递的参数。例如,每个与应用有关的 Action 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
为此 intent 设置目标应用软件包。
android:targetClass
字段指定目的地 activity:com.example.sampleApp.ExerciseActivity
。
- intent
parameter
声明对 BII 参数 exercise.name
的支持,以及如何将从用户处收集的参数值作为 intent
中的 extra 数据进行传递。
将快捷方式与能力相关联
定义某项能力后,您可以通过将静态快捷方式或动态快捷方式与该能力相关联来扩展其功能。快捷方式如何关联到 capability
取决于要实现的功能以及用户请求中包含的实际字词。例如,当用户向 Google 助理发出指令“Hey Google,启动 ExampleApp,开始跑步”,开始在您的健身跟踪应用中跑步时,Google 助理可以使用快捷方式启动一个 capability
实例,该实例为 exercise.name
参数定义有效的“run”运动实体。
如需详细了解如何将快捷方式和与应用有关的 Action 相关联,请参阅与应用有关的 Action 概览。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):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)."]]