尝试运行您的代码是这样的: -
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
上面的代码应该里面的onCreate()来编写,而下面应该写ouside它
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startVoiceRecognitionActivity();
}
}
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
//Run a loop checking whether the list is empty or not:-
while(activities.isEmpty()){
//wait
}
//Now run your alert dialog box
}
我已经测试了DellXCD35的Android 2.3 .3一旦你获得列表中的文本列表,就可以很好地工作,查看它,直到你想要选择。
这[解决方案](http://stackoverflow.com/questions/6316937/how-can-i-use-speech-recognition-without-the-annoying-dialog-in-android-phones)帮助我获得围绕上述问题。 – user492501 2011-12-29 17:29:26