ส่งทางลัดแบบไดนามิกไปยัง Assistant
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แป้นพิมพ์ลัดสำหรับ Android ช่วยให้ผู้ใช้
วิธีดำเนินการหรือเข้าถึงเนื้อหาในแอป
Assistant สามารถแนะนําทางลัดแบบไดนามิกสำหรับ Android ของคุณให้กับผู้ใช้ที่
ที่เกี่ยวข้อง เพื่อช่วยให้ผู้ใช้ค้นพบและเล่นซ้ำ
ฟังก์ชันที่พร้อมใช้งานเสียง
ตัวอย่างเช่น คุณสามารถพุชทางลัดสำหรับโน้ตแต่ละรายการที่ผู้ใช้สร้างขึ้นในแอปจดบันทึกของคุณ คุณทำให้ลิงก์แบบไดนามิกมีสิทธิ์แสดงในแพลตฟอร์มต่างๆ ของ Google เช่น Assistant ได้โดยการเพิ่มคลัง Jetpack สำหรับการผสานรวมทางลัดของ Google ลงในโปรเจ็กต์
ไลบรารีนี้ช่วยให้ Assistant ยอมรับทางลัดแบบไดนามิกที่คุณส่งโดยใช้คลาส ShortcutManagerCompat
ซึ่งเป็น Wrapper ของ Jetpack สําหรับ ShortcutManager
API
เมื่อคุณใช้ไลบรารีการผสานรวม Google Shortcuts ในแอป ผู้ใช้จะเห็นแป้นพิมพ์ลัดแบบไดนามิกที่คุณพุชไปยัง Google เป็นคำแนะนำแป้นพิมพ์ลัดเสียงในแอป Assistant คุณสามารถพุชแป้นพิมพ์ลัดแบบไดนามิกไปยัง Assistant ได้ไม่จำกัดโดยใช้เมธอด pushDynamicShortcut()
ของไลบรารี ShortcutManagerCompat
การเพิ่มฟังก์ชันทางลัดแบบไดนามิกลงในแอปของคุณต้องใช้
ไลบรารีการผสานรวมทางลัดของ Google ซึ่งเป็นไลบรารี Android Jetpack
ส่วนนี้จะอธิบายวิธีกำหนดค่าโปรเจ็กต์การพัฒนาแอปให้รวม
ไลบรารีนี้
หากต้องการเพิ่มไลบรารี Jetpack นี้และกำหนดค่าโปรเจ็กต์ ให้ทำตามขั้นตอนต่อไปนี้
อัปเดตไฟล์ gradle.properties
เพื่อจัดการไลบรารี Jetpack ดังนี้
gradle.properties
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
เพิ่มไลบรารีของ Jetpack ที่ต้องใช้ลงใน build.gradle
โดยทำดังนี้
app/build.gradle
dependencies {
implementation "androidx.core:core:1.6.0"
implementation "androidx.core:core-google-shortcuts:1.0.1"
...
}
ในโค้ดตัวอย่างก่อนหน้านี้ คุณแสดงไลบรารี Jetpack 2 รายการเป็น
ทรัพยากร Dependency ไลบรารี androidx.core:core:1.6.0
ประกอบด้วย
คลาส ShortcutManagerCompat
ซึ่งคุณใช้เพื่อพุชทางลัดแบบไดนามิกไปยัง
Google
androidx.core:core-google-shortcuts:1.0.1
คือกลุ่ม Google
ไลบรารีการผสานรวมทางลัด ไลบรารีนี้ไม่มีหน้าสำหรับนักพัฒนาซอฟต์แวร์
API การเพิ่มเป็น Dependency จะเป็นการเปิดใช้ Assistant ให้รับแป้นพิมพ์ลัดแบบไดนามิกที่คุณส่งโดยใช้คลาส ShortcutManagerCompat
เพิ่มแป้นพิมพ์ลัดแบบไดนามิก
หากต้องการพุชทางลัดแบบไดนามิกที่มีสิทธิ์แสดงใน Assistant ก่อนอื่นคุณต้องสร้างทางลัดโดยใช้คลาส ShortcutInfoCompat.Builder()
จากนั้นพุชทางลัดโดยใช้
ShortcutManagerCompat.pushDynamicShortcut()
วิธี มีการพุชทางลัด
เมื่อใดก็ตามที่ผู้ใช้ดำเนินการที่เกี่ยวข้องในแอปจนเสร็จ ตัวอย่างต่อไปนี้
โค้ดจะพุชทางลัดทุกครั้งที่ผู้ใช้สร้างรายการในแอปโน้ตและลิสต์
ExampleOrderActivity
Kotlin
// Define the dynamic shortcut for an item
var intent = Intent(context, DisplayOrderActivity::class.java)
intent.action = Intent.ACTION_VIEW
var shortcutInfo = ShortcutInfoCompat.Builder(context, id)
.setShortLabel("Running")
.setLongLabel("Start running")
.addCapabilityBinding(
"actions.intent.CREATE_ITEM_LIST", "itemList.name", Arrays.asList("My First List")
)
.setIntent(intent) // Push the shortcut
.build()
// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo)
Java
// Define the dynamic shortcut for an item
Intent intent = new Intent(context, DisplayOrderActivity.class);
intent.setAction(Intent.ACTION_VIEW);
ShortcutInfoCompat.Builder shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
.setShortLabel("Running")
.setLongLabel("Start running")
.addCapabilityBinding(
"actions.intent.CREATE_ITEM_LIST", "itemList.name", Arrays.asList("My First List"))
.setIntent(intent)
.build();
// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);
id
ที่อ้างอิงในเมธอด ShortcutInfoCompat.Builder
ในโค้ดตัวอย่างก่อนหน้าจะกําหนด shortcutId
ของออบเจ็กต์ทางลัดที่ได้ id
ต้องเป็นตัวยึดตำแหน่งสตริงที่ไม่ซ้ำกัน โปรดดูรายละเอียดในเอกสารประกอบเกี่ยวกับทางลัดของ Android
ในตัวอย่างก่อนหน้านี้ เมธอด addCapabilityBinding
จะเชื่อมโยงทางลัดแบบไดนามิกกับ capability
ของ android:name
เดียวกันที่กําหนดไว้ใน shortcuts.xml
วิธีนี้ช่วยให้คุณเชื่อมโยงทางลัดกับพารามิเตอร์ Intent ในตัว (BII) เชิงความหมายได้
บางครั้งระบบจะพุชแป้นพิมพ์ลัดแบบไดนามิกโดยไม่มีพารามิเตอร์ BII
การเชื่อมโยง เมื่อผู้ใช้เรียกใช้ Assistant จะทริกเกอร์ intent
ที่กําหนดไว้ในทางลัดเพื่อดําเนินการ ตัวอย่างต่อไปนี้จะแสดง
ทางลัดที่ไม่มีการเชื่อมโยงพารามิเตอร์
Kotlin
var intent: Intent = Intent(context, DisplayOrderActivity::class.java)
intent.setPackage(this, "com.sample.app")
intent.setAction(Intent.ACTION_VIEW)
var shortcutInfo: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, id)
.setShortLabel("Create a list")
.setLongLabel("Create a list")
.addCapabilityBinding("actions.intent.CREATE_ITEM_LIST")
.setIntent(intent)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);
Java
Intent intent = new Intent(context, DisplayOrderActivity.class);
intent.setPackage(this, "com.sample.app");
intent.setAction(Intent.ACTION_VIEW);
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
.setShortLabel("Create a list")
.setLongLabel("Create a list")
.addCapabilityBinding("actions.intent.CREATE_ITEM_LIST")
.setIntent(intent)
.build();
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);
ทดสอบทางลัดแบบไดนามิกด้วย Assistant
เมื่อ Google Assistant เพิ่มทางลัดแบบไดนามิกจาก
ทางลัดนี้จะมีสิทธิ์ปรากฏเป็นการแนะนำทางลัดด้วยเสียงใน
แอป Assistant สำหรับ Android แอป Assistant แนะนำทางลัดล่าสุด
ที่แอปของคุณพุชเข้ามา
หากต้องการทดสอบทางลัดแบบไดนามิกด้วย Assistant ให้ทำตามขั้นตอนต่อไปนี้
- สร้างตัวอย่างการดำเนินการของแอปและเตรียมอุปกรณ์ทดสอบหรือ
โปรแกรมจำลองสำหรับทดสอบการดำเนินการต่างๆ โดยทำตาม
ที่จำเป็นสำหรับปลั๊กอิน Google Assistant
- เปิดแอปและกำหนดทางลัดแบบไดนามิกเพื่อพุช จากนั้นดําเนินการให้เสร็จสมบูรณ์
เช่น ถ้าคุณพุชทางลัดเมื่อ
ระบบจะสร้างโน้ตในแอปการจดโน้ตของคุณ จากนั้นสร้างโน้ตใหม่
- เปิดทางลัดในแอปการตั้งค่า Assistant บนอุปกรณ์ บัญชี
ทางลัดแบบไดนามิกจะปรากฏในรายการสำหรับแอปของคุณ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],null,["# Push dynamic shortcuts to Assistant\n\n[Android shortcuts](/guide/topics/ui/shortcuts) provide users with quick\nmethods to perform an action or access content in an app.\nAssistant can proactively suggest your Android dynamic shortcuts to users at\nrelevant times, enabling users to easily discover and replay your\nvoice-enabled functionality.\n\nFor example, you can push a shortcut for each note a user creates in\nyour note taking app. You make\ndynamic links eligible to display on Google surfaces, like Assistant,\nby adding the [Google Shortcuts Integration Jetpack library](/jetpack/androidx/releases/core#core-google-shortcuts-1.0.1) to your project.\nThis library lets Assistant take in dynamic shortcuts you push using the\n[`ShortcutManagerCompat`](/reference/androidx/core/content/pm/ShortcutManagerCompat) class, which is a Jetpack wrapper for the\n[`ShortcutManager`](/reference/android/content/pm/ShortcutManager) API.\n\nWhen you use the Google Shortcuts Integration library in your app, dynamic\nshortcuts you push to Google are visible to users as voice shortcut suggestions\nin the Assistant app. You can push an unlimited number of dynamic shortcuts to\nAssistant using the [`pushDynamicShortcut()`](/reference/androidx/core/content/pm/ShortcutManagerCompat#pushDynamicShortcut(android.content.Context,%20androidx.core.content.pm.ShortcutInfoCompat)) method of the\n`ShortcutManagerCompat` library.\n\nConfigure your development project\n----------------------------------\n\nAdding dynamic shortcuts functionality to your app requires the\nGoogle Shortcuts Integration library, which is an Android Jetpack library.\nThis section describes how to configure your app development project to include\nthis library.\n\nTo add this Jetpack library and configure your project, follow these steps:\n\n1. Update your `gradle.properties` file to handle Jetpack libraries:\n\n **gradle.properties** \n\n android.useAndroidX=true\n # Automatically convert third-party libraries to use AndroidX\n android.enableJetifier=true\n\n2. Add the Jetpack library dependencies to your `build.gradle`:\n\n **app/build.gradle** \n\n dependencies {\n implementation \"androidx.core:core:1.6.0\"\n implementation \"androidx.core:core-google-shortcuts:1.0.1\"\n ...\n }\n\n In the preceding sample code, you list two Jetpack libraries as\n dependencies. The `androidx.core:core:1.6.0` library contains the\n `ShortcutManagerCompat` class, which you use to push dynamic shortcuts to\n Google.\n\n The `androidx.core:core-google-shortcuts:1.0.1` is the Google\n Shortcuts Integration library. This library contains no developer-facing\n API. By adding it as a dependency, you enable Assistant to take in the\n dynamic shortcuts you push using the `ShortcutManagerCompat` class.\n | **Note:** Visit the [Jetpack library explorer](/jetpack/androidx/explorer) to find the latest versions of the Core and Google Shortcuts Integration libraries.\n\nPush dynamic shortcuts\n----------------------\n\nTo push dynamic shortcuts that are eligible for display on Assistant,\nyou first create the shortcut using the `ShortcutInfoCompat.Builder()`\nclass.\n\nYou then push the shortcut using the\n`ShortcutManagerCompat.pushDynamicShortcut()` method. Shortcuts are pushed\nwhenever a user completes a relevant action in your app. The following sample\ncode pushes a shortcut every time a user creates a list in a notes and lists app.\n\n**ExampleOrderActivity** \n\n### Kotlin\n\n```kotlin\n// Define the dynamic shortcut for an item\nvar intent = Intent(context, DisplayOrderActivity::class.java)\nintent.action = Intent.ACTION_VIEW\nvar shortcutInfo = ShortcutInfoCompat.Builder(context, id)\n .setShortLabel(\"Running\")\n .setLongLabel(\"Start running\")\n .addCapabilityBinding(\n \"actions.intent.CREATE_ITEM_LIST\", \"itemList.name\", Arrays.asList(\"My First List\")\n )\n .setIntent(intent) // Push the shortcut\n .build()\n\n// Push the shortcut\nShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo)\n```\n\n### Java\n\n```java\n// Define the dynamic shortcut for an item\nIntent intent = new Intent(context, DisplayOrderActivity.class);\nintent.setAction(Intent.ACTION_VIEW);\n\nShortcutInfoCompat.Builder shortcutInfo = new ShortcutInfoCompat.Builder(context, id)\n .setShortLabel(\"Running\")\n .setLongLabel(\"Start running\")\n .addCapabilityBinding(\n \"actions.intent.CREATE_ITEM_LIST\", \"itemList.name\", Arrays.asList(\"My First List\"))\n .setIntent(intent)\n .build();\n\n// Push the shortcut\nShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);\n```\n\nThe `id` referenced in the `ShortcutInfoCompat.Builder` method in the preceding\nsample code defines the `shortcutId` of the resulting shortcut object. This `id`\nmust be a unique string literal. For details, see the\n[Android Shortcuts documentation](/reference/android/content/pm/ShortcutInfo#getId()).\n\nIn the preceding example, the `addCapabilityBinding` method binds the dynamic\nshortcut to a `capability` of the same `android:name` defined in\n`shortcuts.xml`. This method lets you associate the shortcut to a\nsemantic [built-in intent](/guide/app-actions/intents) (BII) parameter.\n\nDynamic shortcuts sometimes are pushed without any particular BII parameter\nassociation. When invoked by the user, Assistant triggers the `intent` defined\nin the shortcut to fulfill the action. The following example shows a dynamic\nshortcut with no parameter association: \n\n### Kotlin\n\n```kotlin\nvar intent: Intent = Intent(context, DisplayOrderActivity::class.java)\nintent.setPackage(this, \"com.sample.app\")\nintent.setAction(Intent.ACTION_VIEW)\n\nvar shortcutInfo: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, id)\n .setShortLabel(\"Create a list\")\n .setLongLabel(\"Create a list\")\n .addCapabilityBinding(\"actions.intent.CREATE_ITEM_LIST\")\n .setIntent(intent)\n .build()\n\nShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);\n```\n\n### Java\n\n```java\nIntent intent = new Intent(context, DisplayOrderActivity.class);\nintent.setPackage(this, \"com.sample.app\");\nintent.setAction(Intent.ACTION_VIEW);\n\nShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, id)\n .setShortLabel(\"Create a list\")\n .setLongLabel(\"Create a list\")\n .addCapabilityBinding(\"actions.intent.CREATE_ITEM_LIST\")\n .setIntent(intent)\n .build();\n\nShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);\n```\n\nTest dynamic shortcuts with Assistant\n-------------------------------------\n\nWhen Google Assistant successfully takes in a dynamic shortcut from your\napplication, the shortcut is eligible to appear as a Voice Shortcut suggestion in the\nAssistant Android app. The Assistant app suggests the most recent shortcuts\npushed by your app.\n\nTo test your dynamic shortcuts with Assistant, follow these steps:\n\n1. Create a preview of your App Actions and prepare your test device or emulator for testing actions by following the same setup requirements as for the [Google Assistant Plugin](/guide/app-actions/test-tool).\n2. Open your app and define a dynamic shortcut to push. Then complete an action. For example, if you push a shortcut whenever a note is created in your note taking app, then create a new note.\n3. Open **Shortcuts** in the **Assistant Settings** app on your device. Your dynamic shortcut appears in the list for your app."]]