推送動態捷徑至 Google 助理
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 捷徑可讓使用者快速執行動作或存取應用程式中的內容。Google 助理可在相關時機主動向使用者建議 Android 動態捷徑,讓使用者輕鬆探索並重播支援語音的功能。
舉例來說,您可以為使用者在記事應用程式中建立的每個記事推送捷徑。只要將 Google 捷徑整合 Jetpack 程式庫新增至專案,即可讓動態連結顯示在 Google 途徑 (例如 Google 助理) 上。這個程式庫可讓 Google 助理使用 ShortcutManagerCompat
類別擷取動態捷徑,而這是 ShortcutManager
API 的 Jetpack 包裝函式。
在應用程式中使用 Google 捷徑整合程式庫時,您推送至 Google 的動態捷徑會顯示在 Google 助理應用程式中,做為語音捷徑建議。您可以使用 ShortcutManagerCompat
程式庫的 pushDynamicShortcut()
方法,將無限數量的動態捷徑推送至 Google 助理。
要新增動態捷徑功能至應用程式,必須有 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 程式庫做為依附元件。androidx.core:core:1.6.0
程式庫包含 ShortcutManagerCompat
類別,可用來將動態捷徑推送至 Google。
androidx.core:core-google-shortcuts:1.0.1
是 Google 捷徑整合程式庫。此程式庫不含任何開發人員專用 API。將此新增為依附元件後,Google 助理就能擷取您使用 ShortcutManagerCompat
類別推送的動態捷徑。
推送動態捷徑
如要推送符合資格,可在 Google 助理中顯示的動態捷徑,您必須先使用 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);
上述程式碼範例中,參考 ShortcutInfoCompat.Builder
方法的 id
會定義產生的捷徑物件 shortcutId
。此id
必須是不重複的字串常值。詳情請參閱「Android 捷徑說明文件」。
在上述範例中,addCapabilityBinding
方法可將動態捷徑繫結至與 shortcuts.xml
中定義的相同 android:name
capability
。這個方法可讓您將捷徑與
語意內建意圖 (BII) 參數。
動態捷徑有時會在沒有特定 BII 參數的情況下推送
。使用者叫用時,Google 助理會觸發捷徑中定義的 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);
透過 Google 助理測試動態捷徑
當 Google 助理成功地從應用程式擷取動態捷徑時,捷徑就符合資格,可在 Google 助理 Android 應用程式中顯示為語音捷徑建議。Google 助理應用程式會建議應用程式推送的最新捷徑。
如要使用 Google 助理測試動態捷徑,請按照下列步驟操作:
- 建立應用程式動作預覽畫面,並準備測試裝置,或
用於測試動作的模擬器
Google 助理外掛程式的安裝需求。
- 請開啟應用程式,並定義要推送的動態捷徑。然後完成動作。舉例來說,如果要在筆記應用程式中建立筆記時推送捷徑,那麼就請建立新的筆記。
- 在裝置上的「Assistant Settings」(Google 助理設定) 應用程式中開啟「Shortcuts」(捷徑)。動態捷徑會顯示在應用程式清單中。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。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,["# 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."]]