How To Make Your Android App Work With The Ok Google Command

I am a big believer in speech interfaces, and probably use them significantly more than the average person thanks to owning an Android Wear device. In fact, I’ve often tried to plug voice to text into any app I design.

 

For this reason, I was absolutely delighted last September when Google announced the ability to bridge the “OK Google” command to your own Android app. I had a demo up and running in barely a few hours, and we released it soon after. I was dead certain that this would be something everyone would want to add to their own app’s experience. I mean, it’s magical. Have a look for yourself with this video we made back then.

 

 

Yet strangely enough I haven’t yet come across any other app that really makes use of it. And like most design patterns, the lack of usage means that this isn’t something users have taken to either. But what is truly the reason for the lack of uptake?

 

Turns out, it’s purely a case of developers and PMs being unaware of it entirely, or of how simple it is to add in the first place. I’m going to show you how you can add this to your app in pretty much five minutes (as long as you already have a search-like experience somewhere in your app).

 

Firstly, a primer on how this works. When a user says, “OK Google, search for <phrase> on <app name>”, Google first checks if there is an app called app name installed which has declared itself to be capable of handling such queries. If there is, the app is launched and the phrase is passed to it. And all this is managed with just a few lines of code in your manifest file and your search activity, as shown in the gist below.

 

<activity
android:name=".SearchActivity">
<intent-filter>
<action android:name="com.google.android.gms.actions.SEARCH_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

 

/**
* Somewhere in your activity
*/
String query = "";
if (getIntent().getAction() != null && getIntent().getAction().equals("com.google.android.gms.actions.SEARCH_ACTION")) {
query = getIntent().getStringExtra(SearchManager.QUERY);
}

 

In the manifest, all you are doing is adding an intent filter to your search activity, using the appropriate action. After that, your activity will be launched and the phrase passed to it. You can then obtain the phrase inside the activity by checking whether the intent’s action matches what we declared, and if so, we extract the appropriate extra.

 

Once you have the phrase, all you have to do is perform the appropriate search within your app. At Haptik, we do this in a unique way by sending the phrase to an expert instead as a message. Not what Google probably expected this to be used as, but powerful nonetheless thanks to its being a hybrid search/texting app. 

 

If you’d love to see your favorite apps add this feature, we recommend you tweet out this blog to them. Do mention us with (@hellohaptik) as well, and we’ll try to assist you in peer pressuring them 🙂

 

Cheers!

Related Articles

View All