如何在Android应用程序中添加语音识别..并使用切换按钮通过它

时间:2016-02-17 11:46:31

标签: android voice-recognition togglebutton

我的应用程序中有一些切换按钮,我需要使用语音命令。例如 - 如果说开关1然后它应该打开开关然后说开关1关闭它应该关闭..感觉非常无奈在此感谢提前帮助我不知道它所以请尝试解释每一个事情非常详细这是我从开发者网站上使用语音识别的代码

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);
}

0 个答案:

没有答案