Thêm chức năng vào lối tắt
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Các tính năng trong shortcuts.xml
cho phép bạn khai báo các loại hành động mà người dùng có thể thực hiện để khởi chạy ứng dụng và thực hiện trực tiếp một tác vụ cụ thể.
Ví dụ: Hành động trong ứng dụng Trợ lý Google sử dụng các tính năng thực hiện tác vụ để hỗ trợ nhà phát triển mở rộng các tính năng trong ứng dụng sang các ý định tích hợp (BII), qua đó cho phép người dùng kích hoạt và kiểm soát các tính năng đó bằng lệnh thoại. Tính năng thực hiện tác vụ bao gồm tên hành động và intent
nhắm mục tiêu đích đến trong ứng dụng mà sẽ giải quyết ý định của người dùng.
Định nghĩa tính năng thực hiện tác vụ trong shortcuts.xml
Bạn định nghĩa các phần tử capability
trong tệp tài nguyên shortcuts.xml
trong dự án phát triển ứng dụng Android. Để xác định một phần tử capability
, hãy làm như sau:
- Tạo tài nguyên
shortcuts.xml
bằng cách làm theo hướng dẫn trong bài viết Tạo lối tắt tĩnh.
Hãy thêm những thông tin bắt buộc sau vào tính năng thực hiện tác vụ:
Tên tính năng thực hiện tác vụ: hành động mà bạn muốn ứng dụng hỗ trợ. Hãy tham khảo tài liệu thành phần để biết về tính năng yêu cầu định nghĩa BII. Các lệnh hỗ trợ bằng giọng nói để thực hiện Hành động trong ứng dụng sẽ sử dụng BII Action ID
làm tên cho các tính năng thực hiện tác vụ. Bạn có thể tìm thấy tên này trong Tài liệu tham khảo về BII. Ví dụ: BII GET_THING
liệt kê Action ID
là actions.intent.GET_THING
.
Đích đến của ứng dụng: đích đến trong ứng dụng mà hành động khởi chạy để hoàn tất yêu cầu của người dùng. Xác định các đích đến của ứng dụng bằng cách sử dụng phần tử intent
được lồng trong capability
.
Liên kết tham số: mỗi intent
có thể chứa các tham số sẽ được truyền dưới dạng dữ liệu extra
của ý định. Ví dụ: mỗi BII của Hành động trong ứng dụng bao gồm các trường đại diện cho thông tin người dùng thường cung cấp trong các lần truy vấn mà kích hoạt BII.
Ví dụ sau đây minh hoạ định nghĩa về tính năng thực hiện tác vụ trong shortcuts.xml
cho actions.intent.START_EXERCISE
, một BII cho phép người dùng sử dụng lệnh thoại thông qua Trợ lý Google để bắt đầu bài tập thể dục trong ứng dụng thể dục:
<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>
Trong ví dụ trước, thuộc tính <capability>
android:name
đề cập đến BII START_EXERCISE
. Nếu người dùng gọi BII này bằng cách hỏi Trợ lý Google, "Ok Google, bắt đầu bài tập chạy trong ExampleApp", Trợ lý sẽ thực hiện yêu cầu của người dùng bằng cách sử dụng
thông tin được cung cấp trong phần tử intent
lồng. intent
trong mẫu này định nghĩa các thông tin chi tiết sau:
android:targetPackage
thiết lập gói ứng dụng mục tiêu cho ý định này.
- Trường
android:targetClass
chỉ định hoạt động tại đích đến:
com.example.sampleApp.ExerciseActivity
.
- Ý định
parameter
khai báo hỗ trợ cho tham số BII exercise.name
và cách truyền giá trị tham số (thu thập từ người dùng) dưới dạng dữ liệu bổ sung trong intent
.
Liên kết lối tắt với một tính năng thực hiện tác vụ
Sau khi xác định một tính năng thực hiện tác vụ, bạn có thể mở rộng chức năng của tính năng đó bằng cách liên kết với lối tắt tĩnh hoặc động. Cách liên kết các lối tắt với một capability
phụ thuộc vào tính năng đang được triển khai và các từ mà người dùng sử dụng khi đưa ra yêu cầu. Ví dụ: khi người dùng bắt đầu một bài tập chạy trong ứng dụng theo dõi tập thể dục bằng cách hỏi Trợ lý, "Ok Google, bắt đầu bài tập chạy trong ExampleApp." Trợ lý có thể sử dụng lối tắt để chạy một thực thể của capability
mà định nghĩa một hoạt động tập thể dục "chạy" hợp lệ cho tham số exercise.name
.
Để biết thêm thông tin về cách liên kết lối tắt với Hành động trong ứng dụng, hãy xem bài viết Tổng quan về Hành động trong ứng dụng.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[],[],null,["Capabilities 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\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\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)."]]