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"?
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.
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