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