android - Google Assistant 语音搜索集成不适用于多个词

标签 android google-voice-search

在我的 Android 项目中,我们最近在文档之后添加了语音搜索。它工作正常,但仅在搜索一个词时有效。这似乎不是预期的行为,因为在 Google 的示例中他们搜索“毛伊岛之旅”。

我们使用 Google Assistant 应用程序(最新版本)在不同设备上尝试了许多搜索命令,并通过 adb 启动。

那么,什么对我们有用: “Ok Google,在 {ourApp} 上搜索巧克力”。

adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION  --es query 'chocolate'

结果:应用程序通过正确的查询在正确的屏幕上启动

什么不起作用: “Ok Google,在 {ourApp} 上搜索冰淇淋”。

adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION  --es query 'ice cream'

结果:Google Assistant 显示网络搜索结果,通过 adb 我们得到:

Starting: Intent { act=com.google.android.gms.actions.SEARCH_ACTION pkg=cream (has extras) }
Error: Activity not started, unable to resolve Intent { act=com.google.android.gms.actions.SEARCH_ACTION flg=0x10000000 pkg=cream (has extras) }

这看起来好像命令不正确,因为系统将“cream”识别为包名称。即使我们在 adb 命令中显式添加包名,结果也是一样的。

我们的集成代码:

<activity
    android:name=".features.search.activities.SearchResultsActivity"
    android:launchMode="singleTask"
    android:screenOrientation="portrait">

    <intent-filter>
        <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

</activity>

然后在 SearchResultsActivity 中:

searchTerm = intent.getStringExtra(SEARCH_TERM_KEY) ?: intent.getStringExtra(SearchManager.QUERY).orEmpty()

如何使用 Google Assistant 实现多词搜索?

最佳答案

我不能告诉你原因(找不到文档),但需要转义空格。

因此,这是您的固定示例:

adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION  --es query 'ice\ cream'

作为解决方法,我编写了以下脚本(名为 send-search.sh):

inputQuery=$(printf %q "$1")
adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION -e query "$inputQuery"

这是你运行它的方式:

sh send-search.sh "ice\ cream"

关于android - Google Assistant 语音搜索集成不适用于多个词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54290654/

相关文章:

android - 最好的Android 2.0开发书?

java - AsyncTask 不显示 onPreExecute

android - WillPopScope无法检测到已按下Android后退按钮

Android EditText 颜色问题

audio - Google Cloud Speech API 可识别哪些音频文件类型?

android - Google 语音识别超时

android - 语音搜索无法在 ok google 上运行

android - 如何显示消息一段时间然后在android中关闭它