How to Implement Voice Command in Android Watch
Voice commands are very important for smart watches as users do not like to
interact with a small screen watch by tapping.In this short article I will explain
how to implement voice command capability in an Android Watch application.
1) App Provided Voice Action.
For example say “RunActivity” is my starting activity for the watch
application.The objective is to provide a voice command like “Start
RunActivity” ,then add “android:label” as below :
<application>
<activity android:name=“.RunActivity” android:label=”RunActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
2) Free form speech input.
In addition to voice command to launch an activity , we can also provide voice
recognizer facility to recognize a voice command and then process it. For
example
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 && 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);
}
Here is the link for the example project.Have fun playing with it.