Some Noob Student
Some Noob Student

Reputation: 14574

How to integrate searchable Activity with Ok Google voice search?

I am trying to implement the Ok Google Voice Search integration. However, I am unable to deeplink into my app when I say "Search for Android on app_name." Instead, it simply searches the term on the web.

Here's what I did:

  1. Create /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. Create a new Activity

    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. Attach intent filters to the searchable activity

    <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. My test device is a Nexus 5 running Lollipop LPX13D with Google Search 4.0.26.1499465.arm

What other steps might I have forgotten? Thanks in advance.

Upvotes: 13

Views: 6030

Answers (2)

viral 9966
viral 9966

Reputation: 525

I have found working solution for this google voice search commands for our Android Application.

Refer below links to make it works:

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

As the above answer listed by Some Noob Student. i am going further for how to test it with debug apk with adding String in search query?

Open command prompt in your PC then change path to your adb path. then execute below commands.

Note: before executing below commands close your debug app then test.

1) adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION -e query app_package_name

2)adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION -e query "Hello" app_package_name

Upvotes: 2

Some Noob Student
Some Noob Student

Reputation: 14574

After lots of searching, I have found the answer in a comment on Google+ by the author of the blog post, Jarek Wilkiewicz.

Yes, the app must be published to the Play Store in order for the feature to work. One way to help debug your end is to trigger the intent via adb, for example: adb shell am start -a com.google.android.gms.actions.SEARCH_ACTION -e query foo

So I tested this feature on an app that is already in the Play Store, and it works flawlessly.

Upvotes: 17

Related Questions