アシスタントに動的ショートカットをプッシュする

Android のショートカットを使用すると、 操作を実行したり、アプリ内のコンテンツにアクセスしたりするためのメソッドです。 アシスタントは、Android の動的ショートカットを ユーザーが簡単に検索して再生できるように 音声対応機能を利用できます。

たとえば、ユーザーが作成したメモごとにショートカットをプッシュ メモ作成アプリ収益額 アシスタントなどの Google サーフェスに表示されるダイナミック リンク Google Shortcuts Integration Jetpack ライブラリをプロジェクトに追加します。 このライブラリを使用すると、 ShortcutManagerCompat クラス: ShortcutManager API。

アプリで Google Shortcuts Integration ライブラリを使用する場合、 Google にプッシュしたショートカットは、音声ショートカットの候補としてユーザーに表示されます 。動的ショートカットをいくつでもプッシュできる Google Cloud の pushDynamicShortcut() メソッドを使用するアシスタント ShortcutManagerCompat ライブラリ。

開発プロジェクトを構成する

動的ショートカット機能をアプリに追加するには、 Google Shortcuts Integration ライブラリ(Android Jetpack ライブラリです)。 このセクションでは、アプリ開発プロジェクトを次のように構成する方法について説明します。 使用できます。

この Jetpack ライブラリを追加してプロジェクトを構成する手順は次のとおりです。

  1. Jetpack ライブラリを処理するように、gradle.properties ファイルを更新します。

    gradle.properties

    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    
  2. Jetpack ライブラリの依存関係を build.gradle に追加します。

    app/build.gradle

    dependencies {
     implementation "androidx.core:core:1.6.0"
     implementation "androidx.core:core-google-shortcuts:1.0.1"
     ...
    }
    

    上記のサンプルコードでは、依存関係として 2 つの Jetpack ライブラリをリストしています。androidx.core:core:1.6.0 ライブラリには、 ShortcutManagerCompat クラス: 動的ショートカットを Google

    androidx.core:core-google-shortcuts:1.0.1 は、Google Shortcuts Integration Library です。このライブラリには、デベロッパー向けの API は含まれていません。依存関係として追加することで、アシスタントは ShortcutManagerCompat クラスを使用してプッシュする動的ショートカット。

動的ショートカットをプッシュする

アシスタントに表示可能な動的ショートカットをプッシュするには、次の操作を行います。 まず、ShortcutInfoCompat.Builder() を使用してショートカットを作成します。 クラスです。

ショートカットをプッシュするには、 ShortcutManagerCompat.pushDynamicShortcut() メソッドを使用します。ショートカットがプッシュされます ユーザーがアプリ内で関連するアクションを完了するたびに次のサンプル ユーザーがフード デリバリー アプリで注文を出すたびに、コードがショートカットをプッシュします。

ExampleOrderActivity

Kotlin

// Define the dynamic shortcut for a menu item
var intent = Intent(context, DisplayOrderActivity::class.java)
intent.action = Intent.ACTION_VIEW
var shortcutInfo = ShortcutInfoCompat.Builder(context, id)
    .setShortLabel("Cappuccino")
    .setLongLabel("Order another cappuccino")
    .addCapabilityBinding(
        "actions.intent.ORDER_MENU_ITEM", "menuItem.name", Arrays.asList("cappuccino")
    )
    .setIntent(intent) // Push the shortcut
    .build()

// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo)

Java

// Define the dynamic shortcut for a menu item
Intent intent = new Intent(context, DisplayOrderActivity.class);
intent.setAction(Intent.ACTION_VIEW);

ShortcutInfoCompat.Builder shortcutInfo = new ShortcutInfoCompat.Builder(context, id)
    .setShortLabel("Cappuccino")
    .setLongLabel("Order another cappuccino")
    .addCapabilityBinding(
      "actions.intent.ORDER_MENU_ITEM", "menuItem.name", Arrays.asList("cappuccino"))
    .setIntent(intent)
    .build();

// Push the shortcut
ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);

上記のサンプルコードの ShortcutInfoCompat.Builder メソッドで参照される id は、生成されるショートカット オブジェクトの shortcutId を定義しています。このid 一意の文字列リテラルにする必要があります。詳しくは、Android ショートカットのドキュメントをご覧ください。

上記の例では、addCapabilityBinding メソッドにより、shortcuts.xml で定義された同じ android:namecapability に動的ショートカットがバインドされています。この方法では、ショートカットを セマンティック 組み込みインテント(BII)パラメータ。

特定の BII パラメータなしで動的ショートカットがプッシュされることがある 関連付けられていますユーザーがアシスタントを呼び出すと、アシスタントはショートカットに定義された 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("Order coffee")
    .setLongLabel("Order a cup of coffee")
    .addCapabilityBinding("actions.intent.ORDER_MENU_ITEM")
    .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("Order coffee")
  .setLongLabel("Order a cup of coffee")
  .addCapabilityBinding("actions.intent.ORDER_MENU_ITEM")
  .setIntent(intent)
  .build();

ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo);

アシスタントで動的ショートカットをテストする

Google アシスタントが動的ショートカットを そのショートカットは、音声ショートカットの候補として Android 版アシスタント アプリ。アシスタント アプリが最新のショートカットを提案します 自動的に適用されます。

アシスタントで動的ショートカットをテストする手順は次のとおりです。

  1. App Actions のプレビューを作成し、テストデバイスを準備するか、 エミュレータを使用して、同じ手順に沿ってアクションをテストします。 Google アシスタント プラグインと同じセットアップ要件があります。
  2. アプリを開き、プッシュする動的ショートカットを定義します。操作を完了します。 たとえば、メモ作成アプリでメモを作成するたびにショートカットをプッシュする場合は、新しいメモを作成します。
  3. デバイスのアシスタント設定アプリで、ショートカットを開きます。お客様の アプリのリストに動的ショートカットが表示されます。