Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...