Android ショートカットを使用すると、ユーザーはアプリ内の操作やコンテンツへのアクセスを簡単に行えます。アシスタントは、適切なタイミングでユーザーに Android の動的ショートカットを提案できるため、ユーザーは音声対応機能を簡単に見つけて再生できるようになります。
たとえば、ユーザーがメモ作成アプリで作成した各メモのショートカットをプッシュできます。プロジェクトに Google Shortcuts Integration Jetpack ライブラリを追加すると、ダイナミック リンクをアシスタントなどの Google サーフェスに表示可能にすることができます。このライブラリにより、アシスタントは ShortcutManagerCompat
クラス(ShortcutManager
API の Jetpack ラッパー)を使用してプッシュした動的ショートカットを取り込むことができます。
アプリで Google Shortcuts Integration Library を使用すると、Google にプッシュした動的ショートカットが、アシスタント アプリで音声ショートカットの候補としてユーザーに表示されます。ShortcutManagerCompat
ライブラリの pushDynamicShortcut()
メソッドを使用すると、動的ショートカットの無制限数でアシスタントにプッシュできます。
開発プロジェクトを構成する
動的ショートカット機能をアプリに追加するには、Android Jetpack ライブラリである Google Shortcuts Integration Library が必要です。このセクションでは、このライブラリを含めるようにアプリ開発プロジェクトを構成する方法について説明します。
この Jetpack ライブラリを追加してプロジェクトを構成する手順は次のとおりです。
Jetpack ライブラリを処理するように、
gradle.properties
ファイルを更新します。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" ... }
上記のサンプルコードでは、依存関係として 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:name
の capability
に動的ショートカットがバインドされています。この方法では、ショートカットをセマンティック組み込みインテント(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 アプリに音声ショートカットの候補として表示される場合があります。アシスタント アプリは、アプリがプッシュした最新のショートカットを提案します。
アシスタントで動的ショートカットをテストする手順は次のとおりです。
- Google アシスタント プラグインと同じ設定要件に沿って、App Actions のプレビューを作成し、アクションをテストするためのテストデバイスまたはエミュレータを準備します。
- アプリを開き、プッシュする動的ショートカットを定義します。操作を完了します。たとえば、メモ作成アプリでメモを作成するたびにショートカットをプッシュする場合は、新しいメモを作成します。
- デバイスのアシスタント設定アプリで、ショートカットを開きます。動的ショートカットがアプリのリストに表示されます。