如何将可搜索的活动与Ok Google语音搜索集成?

时间:2014-11-17 21:50:50

标签: android search

我正在尝试实施Ok Google Voice Search integration。但是,当我说'#34;在app_name上搜索Android时,我无法深入链接到我的应用。"相反,它只是搜索网络上的术语。

这就是我的所作所为:

  1. 创建/res/xml/searchable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint">
    </searchable>
    
  2. 创建新活动

    public class ExposedSearchActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            String search = getIntent().getStringExtra(SearchManager.QUERY);
            Log.wtf("", "q=" + search);
        }
    }
    
  3. 将意图过滤器附加到可搜索的活动

    <activity
        android:name=".search.ExposedSearchActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="fullSensor">
        <!--Deeplink from google now-->
        <intent-filter>
            <action android:name="com.google.android.gms.actions.SEARCH_ACTION"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
        <!--Making it searchable-->
        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>
    
  4. 我的测试设备是运行Lollipop LPX13D的Nexus 5,带有谷歌搜索4.0.26.1499465.arm

  5. 我可能忘记了哪些其他步骤?提前谢谢。

2 个答案:

答案 0 :(得分:16)

经过大量搜索后,我在comment on Google+ by the author of the blog post, Jarek Wilkiewicz找到了答案。

  

是的,该应用必须发布到Play商店才能获得   功能工作。帮助调试结束的一种方法是触发   意图通过adb,例如:adb shell am start -a   com.google.android.gms.actions.SEARCH_ACTION -e query foo

所以我在已经在Play商店中的应用程序上测试了这个功能,它可以完美运行。

答案 1 :(得分:0)

我已经找到了适用于我们Android应用程序的Google语音搜索命令的可行解决方案。

请参阅以下链接以使其起作用:

1)https://gist.github.com/raveeshbhalla/186325d1bb25d13bd7a0

2)https://github.com/google/search-samples/issues/24

3)https://antonioleiva.com/voice_search_google_now/

4)https://developers.google.com/voice-actions/system/

5)https://developer.android.com/guide/components/intents-common#java

Some Noob Student 列出的上述答案。我要走得更远 如何通过在搜索查询中添加String的调试apk对其进行测试?

在PC中打开命令提示符,然后将路径更改为adb路径。然后执行以下命令。

注意:在执行以下命令之前,请关闭调试应用,然后进行测试。

1)adb shell开始-com.google.android.gms.actions.SEARCH_ACTION -e查询app_package_name

2)adb shell开始-com.google.android.gms.actions.SEARCH_ACTION -e查询“ Hello” app_package_name