어시스턴트에 동적 바로가기 푸시

Android 단축키는 작업을 실행하거나 앱의 콘텐츠에 액세스하는 메서드. 어시스턴트가 사전에 사용자에게 Android 동적 바로가기를 추천할 수 있습니다. 이를 통해 사용자는 음성 지원 기능을 제공합니다.

예를 들어 사용자가 작성한 메모마다 바로가기를 푸시할 수 있습니다. 사용할 수 있습니다. 수익 창출 어시스턴트와 같은 Google 표시 경로에 표시할 수 있는 동적 링크 프로젝트에 Google 바로가기 통합 Jetpack 라이브러리를 추가하면 됩니다. 이 라이브러리를 사용하면 내가 다음을 사용하여 푸시한 동적 바로가기를 어시스턴트에서 실행할 수 있습니다. ShortcutManagerCompat 클래스: ShortcutManager API

앱에서 Google 바로가기 통합 라이브러리를 사용하면 동적 Google에 푸시한 단축키가 음성 바로가기 추천으로 사용자에게 표시됩니다. 어시스턴트 앱에서 동적 바로가기를 개수 제한 없이 푸시할 수 있습니다. pushDynamicShortcut() 메서드를 사용하는 어시스턴트 ShortcutManagerCompat 라이브러리.

개발 프로젝트 구성

앱에 동적 바로가기 기능을 추가하려면 Android Jetpack 라이브러리인 Google 바로가기 통합 라이브러리 이 섹션에서는 다음을 포함하도록 앱 개발 프로젝트를 구성하는 방법을 설명합니다. 확인할 수 있습니다

이 Jetpack 라이브러리를 추가하고 프로젝트를 구성하려면 다음 단계를 따르세요.

  1. gradle.properties 파일을 업데이트하여 Jetpack 라이브러리를 처리합니다.

    gradle.properties

    android.useAndroidX=true
    # Automatically convert third-party libraries to use AndroidX
    android.enableJetifier=true
    
  2. build.gradle에 Jetpack 라이브러리 종속 항목을 추가합니다.

    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가 포함되어 있지 않습니다. 이를 종속 항목으로 추가하면 어시스턴트가 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)

자바

// 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);

자바

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. 앱 작업의 미리보기를 만들고 테스트 기기를 준비합니다. 다음과 같이 동일한 방식으로 작업을 테스트하는 에뮬레이터 Google 어시스턴트 플러그인 설정 요구사항
  2. 앱을 열고 푸시할 동적 바로가기를 정의합니다. 작업을 완료합니다. 예를 들어 메모 작성 앱에 메모가 생성될 때마다 바로가기를 푸시하는 경우 새 메모를 만듭니다.
  3. 기기의 어시스턴트 설정 앱에서 바로가기를 엽니다. 내 앱 목록에 동적 바로가기가 표시됩니다.