1page.title=Adding Voice Capabilities 2page.tags=wear 3helpoutsWidget=true 4 5@jd:body 6 7<div id="tb-wrapper"> 8 <div id="tb"> 9 10 <!-- Required platform, tools, add-ons, devices, knowledge, etc. --> 11 <h2>This lesson teaches you to</h2> 12 <ol> 13 <li><a href="#SystemProvided">Declare System-provided Voice Actions</a></li> 14 <li><a href="#AppProvided">Declare App-provided Voice Actions</a></li> 15 <li><a href="#FreeFormSpeech">Obtaining Free-form Speech Input</a></li> 16 </ol> 17 <h2>You should also read</h2> 18 <ul> 19 <li><a href="{@docRoot}design/wear/index.html">Android Wear Design Principles</a></li> 20 </ul> 21 </div> 22</div> 23 24<p>Voice actions are an important part of the wearable experience. They let users carry 25out actions hands-free and quickly. Wear provides two types of voice actions:</p> 26 27<dl> 28 <dt><b>System-provided</b></dt> 29 <dd>These voice actions are task-based and are built 30 into the Wear platform. You filter for them in the activity that you want to start when the 31 voice action is spoken. Examples include "Take a note" or "Set an alarm".</dd> 32 <dt><b>App-provided</b></dt> 33 <dd>These voice actions are app-based, and you declare them just like a launcher icon. 34 Users say "Start <Your App Name>" to use these voice actions and an activity that you specify 35 starts.</dd> 36</dl> 37 38<h2 id="SystemProvided" style="clear:right">Declare System-provided Voice Actions</h2> 39<p> 40The Android Wear platform provides several voice intents that are based on user actions such 41as "Take a note" or "Set an alarm". This allows users to say what they want to do and let 42the system figure out the best activity to start.</p> 43 44<p>When users speak the voice action, your app can filter for the intent that is fired to start 45an activity. If you want to start a service to do something in the background, show an activity as 46a visual cue and start the service in the activity. Make sure to call 47{@link android.app.Activity#finish finish()} when you want to get rid of the visual cue. 48</p> 49 50<p>For example, for the "Take a note" command, declare this intent filter to start an activity 51named <code>MyNoteActivity</code>: 52</p> 53 54<pre> 55 <activity android:name="MyNoteActivity"> 56 <intent-filter> 57 <action android:name="android.intent.action.SEND" /> 58 <category android:name="com.google.android.voicesearch.SELF_NOTE" /> 59 </intent-filter> 60 </activity> 61</pre> 62 63<p>Here is a list of the voice intents supported by the Wear platform:</p> 64 65<table> 66 <tr> 67 <th>Name</th> 68 <th>Example Phrases</th> 69 <th>Intent</th> 70 </tr> 71 72 <tr> 73 <td>Call a car/taxi</td> 74 <td>"OK Google, get me a taxi"<br/><br/>"OK Google, call me a car"</td> 75 <td> 76 <dl> 77 <dt>Action</dt> 78 <dd> 79 <code>com.google.android.gms.actions.RESERVE_TAXI_RESERVATION</code> 80 </dd> 81 </dl> 82 </td> 83 </tr> 84 85 <tr> 86 <td>Take a note</td> 87 <td>"OK Google, take a note"<br/><br/>"OK Google, note to self"</td> 88 <td> 89 <dl> 90 <dt>Action</dt> 91 <dd><code>android.intent.action.SEND</code></dd> 92 <dt>Category</dt> 93 <dd><code>com.google.android.voicesearch.SELF_NOTE</code></dd> 94 <dt>Extras</dt> 95 <dd><code>android.content.Intent.EXTRA_TEXT</code> - a string with note body</dd> 96 </dl> 97 </td> 98 </tr> 99 100 <tr> 101 <td>Set alarm</td> 102 <td>"OK Google, set an alarm for 8 AM"<br/><br/>"OK Google, wake me up at 6 tomorrow"</td> 103 <td> 104 <dl> 105 <dt>Action</dt> 106 <dd><code>android.intent.action.SET_ALARM</code></dd> 107 <dt>Extras</dt> 108 <dd><code>android.provider.AlarmClock.EXTRA_HOUR</code> - an integer with the hour of 109 the alarm. 110 <p><code>android.provider.AlarmClock.EXTRA_MINUTES</code> - 111 an integer with the minute of the alarm 112 <p>(these 2 extras are optional, either none or 113 both are provided)</p></dd> 114 115 </dl> 116 </td> 117 </tr> 118 119 <tr> 120 <td>Set timer</td> 121 <td>"Ok Google, set a timer for 10 minutes"</td> 122 <td> 123 <dl> 124 <dt>Action</dt> 125 <dd><code>android.intent.action.SET_TIMER</code></dd> 126 <dt>Extras</dt> 127 <dd><code>android.provider.AlarmClock.EXTRA_LENGTH</code> - an integer in the range of 128 1 to 86400 (number of seconds in 24 hours) representing the length of the timer </dd> 129 </dl> 130 </td> 131 </tr> 132 133 <tr> 134 <td>Start/Stop a bike ride</td> 135 <td>"OK Google, start cycling"<br/><br/>"OK Google, start my bike ride"<br/><br/>"OK Google, stop cycling"</td> 136 <td> 137 <dl> 138 <dt>Action</dt> 139 <dd><code>vnd.google.fitness.TRACK</code></dd> 140 <dt>Mime Type</dt> 141 <dd><code>vnd.google.fitness.activity/biking</code></dd> 142 <dt>Extras</dt> 143 <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code> 144 when starting and <code>CompletedActionStatus</code> when stopping.</dd> 145 </dl> 146 </td> 147 </tr> 148 149 <tr> 150 <td>Start/Stop a run</td> 151 <td>"OK Google, track my run"<br/><br/>"OK Google, start running"<br/><br/>"OK Google, stop running"</td> 152 <td> 153 <dl> 154 <dt>Action</dt> 155 <dd><code>vnd.google.fitness.TRACK</code></dd> 156 <dt>MimeType</dt> 157 <dd><code>vnd.google.fitness.activity/running</code></dd> 158 <dt>Extras</dt> 159 <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code> 160 when starting and <code>CompletedActionStatus</code> when stopping</dd> 161 </dl> 162 </td> 163 </tr> 164 165 166 <tr> 167 <td>Start/Stop a workout</td> 168 <td>"OK Google, start a workout"<br/><br/>"OK Google, track my workout"<br/><br/>"OK Google, stop workout"</td> 169 <td> 170 <dl> 171 <dt>Action</dt> 172 <dd><code>vnd.google.fitness.TRACK</code></dd> 173 <dt>MimeType</dt> 174 <dd><code>vnd.google.fitness.activity/other</code></dd> 175 <dt>Extras</dt> 176 <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code> 177 when starting and <code>CompletedActionStatus</code> when stopping</dd> 178 </dd> 179 </dl> 180 </td> 181 </tr> 182 183 <tr> 184 <td>Show heart rate</td> 185 <td>"OK Google, what’s my heart rate?"<br/><br/>"OK Google, what’s my bpm?"</td> 186 <td> 187 <dl> 188 <dt>Action</dt> 189 <dd><code>vnd.google.fitness.VIEW</code></dd> 190 <dt>Mime Type</dt> 191 <dd><code>vnd.google.fitness.data_type/com.google.heart_rate.bpm</code></dd> 192 </dd> 193 </dl> 194 </td> 195 </tr> 196 197 <tr> 198 <td>Show step count</td> 199 <td>"OK Google, how many steps have I taken?"<br/><br/>"OK Google, what’s my step count?"</td> 200 <td> 201 <dl> 202 <dt>Action</dt> 203 <dd><code>vnd.google.fitness.VIEW</code></dd> 204 <dt>Mime Type</dt> 205 <dd><code>vnd.google.fitness.data_type/com.google.step_count.cumulative</code></dd> 206 </dd> 207 </dl> 208 </td> 209 </tr> 210 211</table> 212 213<p> 214For documentation on registering for platform intents and accessing the extras information 215contained in them, see <a href="{@docRoot}guide/components/intents-common.html">Common intents</a>. 216</p> 217 218<h2 id="AppProvided">Declare App-provided Voice Actions</h2> 219<p> 220If none of the platform voice intents work for you, you can start your apps directly with 221a "Start MyActivityName" voice action. </p> 222 223<p>Registering for a "Start" action is the same as registering 224for a launcher icon on a handheld. Instead of requesting an app icon in a launcher, 225your app requests a voice action instead.</p> 226 227<p>To specify the text to say after "Start", specify a <code>label</code> attribute for the activtiy 228that you want to start. For example, this intent filter recognizes the 229"Start MyRunningApp" voice action and launches <code>StartRunActivity</code>. 230</p> 231 232<pre> 233<application> 234 <activity android:name="StartRunActivity" android:label="MyRunningApp"> 235 <intent-filter> 236 <action android:name="android.intent.action.MAIN" /> 237 <category android:name="android.intent.category.LAUNCHER" /> 238 </intent-filter> 239 </activity> 240</application> 241</pre> 242 243<h2 id="FreeFormSpeech">Obtaining Free-form Speech Input</h2> 244<p>In addition to using voice actions to launch activities, you can also call the system's 245built-in Speech Recognizer activity to obtain speech input from users. This is useful to obtain input 246from users and then process it, such as doing a search or sending it as a message.</p> 247 248In your app, you call {@link android.app.Activity#startActivityForResult startActivityForResult()} using 249the {@link android.speech.RecognizerIntent#ACTION_RECOGNIZE_SPEECH} action. This starts the 250speech recognition activity, and you can then handle the result 251in {@link android.app.Activity#onActivityResult onActivityResult()}. 252<pre> 253private static final int SPEECH_REQUEST_CODE = 0; 254 255// Create an intent that can start the Speech Recognizer activity 256private void displaySpeechRecognizer() { 257 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 258 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, 259 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 260// Start the activity, the intent will be populated with the speech text 261 startActivityForResult(intent, SPEECH_REQUEST_CODE); 262} 263 264// This callback is invoked when the Speech Recognizer returns. 265// This is where you process the intent and extract the speech text from the intent. 266@Override 267protected void onActivityResult(int requestCode, int resultCode, 268 Intent data) { 269 if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) { 270 List<String> results = data.getStringArrayListExtra( 271 RecognizerIntent.EXTRA_RESULTS); 272 String spokenText = results.get(0); 273 // Do something with spokenText 274 } 275 super.onActivityResult(requestCode, resultCode, data); 276} 277</pre> 278