从Google即时接收搜索

时间:2014-11-03 16:06:08

标签: android search android-intent android-searchmanager google-now

所以我尝试实现这个 - The fastest route between voice search and your app

到目前为止我所拥有的是......

在清单中:

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

在MainActivity中:

if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        editText.setText(query);

}

如果我在Google即时中输入我的应用名称,则会显示该应用。如果我打开它没有任何反应,所以我没有收到搜索词(应用程序名称)。

如何实现帖子描述“Ok Google,在MyApp上搜索披萨”的方式?

2 个答案:

答案 0 :(得分:2)

根据blog post,查询格式为:

  

Ok Google,在Eat24上搜索披萨

     

Ok Google,在TripAdvisor上搜索毛伊岛的酒店

您会注意到粗体部分是您的应用在以正确格式向您的应用发送成功的语音搜索时收到的搜索字词。

答案 1 :(得分:1)

另外,我们检查Intent的方式也可能不同。只有这对我有用。正如文档中提到的意图是这样。

Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())
        || "com.google.android.gms.actions.SEARCH_ACTION".equals(intent.getAction())) {
    externalQuery = intent.getStringExtra(SearchManager.QUERY);
    //String xquery = query;
}
相关问题