如何在Android中使用语音命令API

时间:2011-07-29 12:08:55

标签: android

我是android的新手,目前正在开发适用于Voice Command API的小型应用程序。例如,如果我说蓝牙,它会将手机的蓝牙切换到开/关模式(反之亦然)。

请帮我这样做....

感谢Anvance ...

1 个答案:

答案 0 :(得分:10)

使用起来相当简单:

private void startVoiceRecognitionActivity() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    //uses free form text input
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //Puts a customized message to the prompt
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
        getString(R.string.listenprompt));
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

/**
 * Handles the results from the recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
        // Fill the list view with the strings the recognizer thought it could have heard
        ArrayList<String> matches = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);

        //Turn on or off bluetooth here
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

然后在代码中的任何地方调用startVoiceRecognitionActivity()。当然,你需要有权利访问互联网

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

在你的Android.manifest。