Receive search from Google Now

≯℡__Kan透↙ 提交于 2019-12-04 15:58:08

问题


So I tried to implement this - The fastest route between voice search and your app

What i have so far is...

In manifest:

<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>

In MainActivity:

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

}

If I type my app name in Google Now, the app is shown. If I open it nothing happens, so I didn't receive the search term (the app name).

How do I implement like how the post has described "Ok Google, search pizza on MyApp"?


回答1:


Per the blog post, queries are in the format:

Ok Google, search pizza on Eat24

Ok Google, search for hotels in Maui on TripAdvisor

You'll note the bolded portion is the search term your app receives when a successful voice search in the correct format is sent to your app.




回答2:


Also, the way we check for Intent also may be different. Only this worked for me. As the intent mentioned in the documentation is this.

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;
}


来源:https://stackoverflow.com/questions/26718139/receive-search-from-google-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!