尝试以这种方式运行您的代码:-
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() 里面,下面的代码应该写在外面
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 上对其进行了测试,一旦您在列表视图中获得文本列表,它就可以很好地工作,这取决于您想要选择的内容。