在上面的代码中,您只是测试常量是否为空,而它们不是。您必须通过以下方式在代码中的某处开始识别
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//... put other settings in the Intent
startActivityForResult(intent, REQUEST_CODE);
并收到结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> results = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
//... do your stuf with results
}
super.onActivityResult(requestCode, resultCode, data);
}
一种更可定制的方式是直接使用 SpeechRecognizer。例如看这个问题。