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  &lt;activity android:name="MyNoteActivity"&gt;
56      &lt;intent-filter&gt;
57          &lt;action android:name="android.intent.action.SEND" /&gt;
58          &lt;category android:name="com.google.android.voicesearch.SELF_NOTE" /&gt;
59      &lt;/intent-filter&gt;
60  &lt;/activity&gt;
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 stopwatch</td>
135    <td>"Ok Google, start stopwatch"</td>
136    <td>
137      <dl>
138        <dt>Action</dt>
139        <dd><code>com.google.android.wearable.action.STOPWATCH</code></dd>
140      </dl>
141   </td>
142  </tr>
143
144  <tr>
145    <td>Start/Stop a bike ride</td>
146    <td>"OK Google, start cycling"<br/><br/>"OK Google, start my bike ride"<br/><br/>"OK Google, stop cycling"</td>
147    <td>
148      <dl>
149        <dt>Action</dt>
150        <dd><code>vnd.google.fitness.TRACK</code></dd>
151        <dt>Mime Type</dt>
152        <dd><code>vnd.google.fitness.activity/biking</code></dd>
153        <dt>Extras</dt>
154        <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code>
155        when starting and <code>CompletedActionStatus</code> when stopping.</dd>
156      </dl>
157   </td>
158  </tr>
159
160  <tr>
161    <td>Start/Stop a run</td>
162    <td>"OK Google, track my run"<br/><br/>"OK Google, start running"<br/><br/>"OK Google, stop running"</td>
163    <td>
164      <dl>
165        <dt>Action</dt>
166        <dd><code>vnd.google.fitness.TRACK</code></dd>
167        <dt>MimeType</dt>
168        <dd><code>vnd.google.fitness.activity/running</code></dd>
169        <dt>Extras</dt>
170        <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code>
171        when starting and <code>CompletedActionStatus</code> when stopping</dd>
172      </dl>
173   </td>
174  </tr>
175
176
177  <tr>
178    <td>Start/Stop a workout</td>
179    <td>"OK Google, start a workout"<br/><br/>"OK Google, track my workout"<br/><br/>"OK Google, stop workout"</td>
180    <td>
181      <dl>
182        <dt>Action</dt>
183        <dd><code>vnd.google.fitness.TRACK</code></dd>
184        <dt>MimeType</dt>
185        <dd><code>vnd.google.fitness.activity/other</code></dd>
186        <dt>Extras</dt>
187        <dd><code>actionStatus</code> - a string with the value <code>ActiveActionStatus</code>
188        when starting and <code>CompletedActionStatus</code> when stopping</dd>
189        </dd>
190      </dl>
191   </td>
192  </tr>
193
194  <tr>
195    <td>Show heart rate</td>
196    <td>"OK Google, what’s my heart rate?"<br/><br/>"OK Google, what’s my bpm?"</td>
197    <td>
198      <dl>
199        <dt>Action</dt>
200        <dd><code>vnd.google.fitness.VIEW</code></dd>
201        <dt>Mime Type</dt>
202        <dd><code>vnd.google.fitness.data_type/com.google.heart_rate.bpm</code></dd>
203        </dd>
204      </dl>
205   </td>
206  </tr>
207
208  <tr>
209    <td>Show step count</td>
210    <td>"OK Google, how many steps have I taken?"<br/><br/>"OK Google, what’s my step count?"</td>
211    <td>
212      <dl>
213        <dt>Action</dt>
214        <dd><code>vnd.google.fitness.VIEW</code></dd>
215        <dt>Mime Type</dt>
216        <dd><code>vnd.google.fitness.data_type/com.google.step_count.cumulative</code></dd>
217        </dd>
218      </dl>
219   </td>
220  </tr>
221
222</table>
223
224<p>
225For documentation on registering for platform intents and accessing the extras information
226contained in them, see <a href="{@docRoot}guide/components/intents-common.html">Common intents</a>.
227</p>
228
229<h2 id="AppProvided">Declare App-provided Voice Actions</h2>
230<p>
231If none of the platform voice intents work for you, you can start your apps directly with
232a "Start MyActivityName" voice action. </p>
233
234<p>Registering for a "Start" action is the same as registering
235for a launcher icon on a handheld. Instead of requesting an app icon in a launcher,
236your app requests a voice action instead.</p>
237
238<p>To specify the text to say after "Start", specify a <code>label</code> attribute for the activtiy
239that you want to start. For example, this intent filter recognizes the
240"Start MyRunningApp" voice action and launches <code>StartRunActivity</code>.
241</p>
242
243<pre>
244&lt;application&gt;
245  &lt;activity android:name="StartRunActivity" android:label="MyRunningApp"&gt;
246      &lt;intent-filter&gt;
247          &lt;action android:name="android.intent.action.MAIN" /&gt;
248          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
249      &lt;/intent-filter&gt;
250  &lt;/activity&gt;
251&lt;/application&gt;
252</pre>
253
254<h2 id="FreeFormSpeech">Obtaining Free-form Speech Input</h2>
255<p>In addition to using voice actions to launch activities, you can also call the system's
256built-in Speech Recognizer activity to obtain speech input from users. This is useful to obtain input
257from users and then process it, such as doing a search or sending it as a message.</p>
258
259In your app, you call {@link android.app.Activity#startActivityForResult startActivityForResult()} using
260the {@link android.speech.RecognizerIntent#ACTION_RECOGNIZE_SPEECH} action. This starts the
261speech recognition activity, and you can then handle the result
262in {@link android.app.Activity#onActivityResult onActivityResult()}.
263<pre>
264private static final int SPEECH_REQUEST_CODE = 0;
265
266// Create an intent that can start the Speech Recognizer activity
267private void displaySpeechRecognizer() {
268    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
269    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
270            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
271// Start the activity, the intent will be populated with the speech text
272    startActivityForResult(intent, SPEECH_REQUEST_CODE);
273}
274
275// This callback is invoked when the Speech Recognizer returns.
276// This is where you process the intent and extract the speech text from the intent.
277&#64;Override
278protected void onActivityResult(int requestCode, int resultCode,
279        Intent data) {
280    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
281        List&lt;String&gt; results = data.getStringArrayListExtra(
282                RecognizerIntent.EXTRA_RESULTS);
283        String spokenText = results.get(0);
284        // Do something with spokenText
285    }
286    super.onActivityResult(requestCode, resultCode, data);
287}
288</pre>
289