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

Som*_*ent 11 search android

我正在尝试实施Ok Google Voice Search集成.但是,当我说"在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>
    
    Run Code Online (Sandbox Code Playgroud)
  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);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 我的测试设备是运行Lollipop LPX13D的Nexus 5,带有谷歌搜索4.0.26.1499465.arm

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

Som*_*ent 17

经过大量搜索后,我在博客文章作者Jarek Wilkiewicz评论中找到了答案.

是的,必须将应用程序发布到Play商店才能使该功能正常运行.帮助调试结束的一种方法是通过adb触发意图,例如:adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION -e query foo

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