考虑到官方文档和代码实验室中显示的选项,我的问题相对简单,但我一直在努力使其工作。
我想通过动态快捷方式触发一个小部件在 Google Assistant 中显示。看起来非常简单,但是当实现这样的功能和动态快捷方式时:
<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>
我可以省略意图,触发此操作会将逻辑按预期委托给功能的小部件。不幸的是,我无法找到一种方法来创建没有意图的动态快捷方式。
我这里缺少什么吗?感谢帮助。