<等待翻译>Android Wear 进阶 2.4 Adding Voice Capabilities 添加语音功能

Adding Voice Capabilities

Voice actions are an important part of the wearable experience. They let users carry out actions hands-free and quickly. Wear provides two types of voice actions:

System-provided
These voice actions are task-based and are built into the Wear platform. You filter for them in the activity that you want to start when the voice action is spoken. Examples include "Take a note" or "Set an alarm".
App-provided
These voice actions are app-based, and you declare them just like a launcher icon. Users say "Start " to use these voice actions and an activity that you specify starts.

Declare System-provided Voice Actions



The Android Wear platform provides several voice intents that are based on user actions such as "Take a note" or "Set an alarm". This allows users to say what they want to do and let the system figure out the best activity to start.

When users speak the voice action, your app can filter for the intent that is fired to start an activity. If you want to start a service to do something in the background, show an activity as a visual cue and start the service in the activity. Make sure to call finish() when you want to get rid of the visual cue.

For example, for the "Take a note" command, declare this intent filter to start an activity namedMyNoteActivity:

  <activity android:name="MyNoteActivity">
      <intent-filter>
          <action android:name="android.intent.action.SEND" />
          <category android:name="com.google.android.voicesearch.SELF_NOTE" />
      </intent-filter>
  </activity>

Here is a list of the voice intents supported by the Wear platform:

Name Example Phrases Intent
Call a car/taxi "OK Google, get me a taxi"

"OK Google, call me a car"
Action
com.google.android.gms.actions.RESERVE_TAXI_RESERVATION
Take a note "OK Google, take a note"

"OK Google, note to self"
Action
android.intent.action.SEND
Category
com.google.android.voicesearch.SELF_NOTE
Extras
android.content.Intent.EXTRA_TEXT - a string with note body
Set alarm "OK Google, set an alarm for 8 AM"

"OK Google, wake me up at 6 tomorrow"
Action
android.intent.action.SET_ALARM
Extras
android.provider.AlarmClock.EXTRA_HOUR - an integer with the hour of the alarm.

android.provider.AlarmClock.EXTRA_MINUTES - an integer with the minute of the alarm

(these 2 extras are optional, either none or both are provided)

Set timer "Ok Google, set a timer for 10 minutes"
Action
android.intent.action.SET_TIMER
Extras
android.provider.AlarmClock.EXTRA_LENGTH - an integer in the range of 1 to 86400 (number of seconds in 24 hours) representing the length of the timer
Start stopwatch "Ok Google, start stopwatch"
Action
com.google.android.wearable.action.STOPWATCH
Start/Stop a bike ride "OK Google, start cycling"

"OK Google, start my bike ride"

"OK Google, stop cycling"
Action
vnd.google.fitness.TRACK
Mime Type
vnd.google.fitness.activity/biking
Extras
actionStatus - a string with the value  ActiveActionStatus when starting and  CompletedActionStatus when stopping.
Start/Stop a run "OK Google, track my run"

"OK Google, start running"

"OK Google, stop running"
Action
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/running
Extras
actionStatus - a string with the value  ActiveActionStatus when starting and  CompletedActionStatus when stopping
Start/Stop a workout "OK Google, start a workout"

"OK Google, track my workout"

"OK Google, stop workout"
Action
vnd.google.fitness.TRACK
MimeType
vnd.google.fitness.activity/other
Extras
actionStatus - a string with the value  ActiveActionStatus when starting and  CompletedActionStatus when stopping
Show heart rate "OK Google, what’s my heart rate?"

"OK Google, what’s my bpm?"
Action
vnd.google.fitness.VIEW
Mime Type
vnd.google.fitness.data_type/com.google.heart_rate.bpm
Show step count "OK Google, how many steps have I taken?"

"OK Google, what’s my step count?"
Action
vnd.google.fitness.VIEW
Mime Type
vnd.google.fitness.data_type/com.google.step_count.cumulative

For documentation on registering for platform intents and accessing the extras information contained in them, see Common intents.

Declare App-provided Voice Actions



If none of the platform voice intents work for you, you can start your apps directly with a "Start MyActivityName" voice action.

Registering for a "Start" action is the same as registering for a launcher icon on a handheld. Instead of requesting an app icon in a launcher, your app requests a voice action instead.

To specify the text to say after "Start", specify a label attribute for the activtiy that you want to start. For example, this intent filter recognizes the "Start MyRunningApp" voice action and launches StartRunActivity.

<application>
  <activity android:name="StartRunActivity" android:label="MyRunningApp">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
  </activity>
</application>

Obtaining Free-form Speech Input



In addition to using voice actions to launch activities, you can also call the system's built-in Speech Recognizer activity to obtain speech input from users. This is useful to obtain input from users and then process it, such as doing a search or sending it as a message.

In your app, you call  startActivityForResult() using the  ACTION_RECOGNIZE_SPEECH action. This starts the speech recognition activity, and you can then handle the result in  onActivityResult().
private static final int SPEECH_REQUEST_CODE = 0;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
        String spokenText = results.get(0);
        // Do something with spokenText
    }
    super.onActivityResult(requestCode, resultCode, data);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值