Here's how to harness the power of Google Now in your Android app — without writing Java code — by letting users launch your app using voice commands.
Let's face it: Google Now is so cool. I remember working on an application 10 years ago that had "voice recognition." The room needed to be entirely quiet, you had to annunciate like a Speak & Spell, and just maybe, if you shouted at the speed of paint drying, the underlying algorithm had a fifty-fifty shot at understanding what you were trying to tell it.
Google Now, on the other hand, not only almost always gets it right, but also manages to do so in a crowded restaurant when others are talking and music is blaring on the patio. Wouldn't it be great to harness that power in your own app? Well, you can. In fact, as of JellyBean you can let users launch your app using voice commands without writing any Java code. This tutorial will teach you how.
Follow along using the step-by-step instructions below, or download and import the entire project directly into Android Studio.
1. Create a new Android project in Android Studio. Be sure to target JellyBean (API 19) or better.
2. In the /res folder, create a new directory called /xml. Add a file to the newly created directory titled searchable.xml. Use the label node in searchable.xml to call out your app by name.
searchable.xml <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="My Voice Activated App" > </searchable>
3. In the /manifests/AndroidManifest.xml file, add a searchable intent and metadata to your main activity.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.authorwjf.myvoiceactivatedapp" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.authorwjf.myvoiceactivatedapp.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now you're ready to give it a go! Enable Google Now everywhere on your device, if you haven't already. Load the app to your device, and then exit the app or push it to the background. Say: "Okay Google, start my voice activated app." Providing everything went according to plan, your app should launch.
Congratulations — you've harnessed the power of Google Now.