App Actions:通过动态快捷方式在 Google Assistant 中显示小部件

问题描述 投票:0回答:1

考虑到官方文档和代码实验室中显示的选项,我的问题相对简单,但我一直在努力使其工作。

我想触发一个小部件通过动态快捷方式显示在 Google 智能助理中。看起来很简单,但是在实现这样的功能和动态快捷方式时:

<capability
    android:name="actions.intent.GET_RESERVATION">
    <app-widget
        android:identifier="GET_MY_RESERVATION"
        android:targetClass="com.myapp.widget.MyWidget">
        <parameter
            android:name="reservation.reservationFor.name"
            android:key="shortcutId"
            android:required="true"
            app:shortcutMatchRequired="true" />
        <extra android:name="hasTts" android:value="true"/>
    </app-widget>
    <intent
        android:identifier="GET_MY_RESERVATION_FALLBACK"
        android:action="android.intent.action.VIEW"
        android:targetClass="com.myapp.widget.MyWidget">
    </intent>
</capability>
val shortcut = ShortcutInfoCompat.Builder(context, "shortcut_id")
        .setShortLabel("shortcut label"))
        .setExcludedFromSurfaces(ShortcutInfoCompat.SURFACE_LAUNCHER)
        .setLongLived(false)
        .addCapabilityBinding(
            "actions.intent.GET_RESERVATION",
            "reservation.reservationFor.name",
            context.resources.getStringArray(R.array.synonyms).toList()
        )
        .setIntent(Intent(context, MyWidget::class.java).apply {
            action = Intent.ACTION_VIEW
        })
        .setRank(2)
        .build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)

为快捷方式定义的意图是被触发的,而不是为功能定义的小部件。此外,上面的快捷方式定义使应用程序崩溃,因为它期望它的意图是可以通过

startActivity()
调用的东西(而小部件不是)。

如果我静态定义相同的快捷方式:

<shortcut
    android:shortcutId="shortcut_id"
    android:shortcutShortLabel="shortcut label">
    <capability-binding
        android:key="actions.intent.GET_RESERVATION">
        <parameter-binding
            android:key="reservation.reservationFor.name"
            android:value="@array/synonyms" />
    </capability-binding>
</shortcut>

我可以省略意图并触发此操作将按预期将逻辑委托给功能的小部件。不幸的是,我找不到一种方法来创建一个没有意图的动态快捷方式。

我在这里遗漏了什么吗?感谢帮助。

android actions-on-google google-assistant app-actions android-dynamic-shortcuts
1个回答
0
投票

代码的问题是动态快捷方式是用指向小部件类的意图定义的,但是 ShortcutManagerCompat.pushDynamicShortcut 方法需要一个可以通过 startActivity() 启动的意图。要解决这个问题,您需要定义一个启动活动的意图,并且在该活动中,您可以显示小部件。

val shortcut = ShortcutInfoCompat.Builder(context, "shortcut_id")
        .setShortLabel("shortcut label"))
        .setExcludedFromSurfaces(ShortcutInfoCompat.SURFACE_LAUNCHER)
        .setLongLived(false)
        .addCapabilityBinding(
            "actions.intent.GET_RESERVATION",
            "reservation.reservationFor.name",
            context.resources.getStringArray(R.array.synonyms).toList()
        )
        .setIntent(Intent(context, MyActivity::class.java).apply {
            action = Intent.ACTION_VIEW
            putExtra("widgetId", R.id.my_widget) // pass the ID of the widget to be displayed
        })
        .setRank(2)
        .build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)

小部件通过您创建的活动 MyActivity 显示。此活动由传递给 ShortcutInfoCompat.Builder 的意图启动,并且小部件 ID 可以作为额外传递。通过从 intent 的 extras 中检索小部件 ID,您可以在 MyActivity 中显示它。

© www.soinside.com 2019 - 2024. All rights reserved.