是否可以通过编程方式打开“立即说出”对话框?
目前,如果用户点击我的“搜索”按钮,会打开一个对话框,我会自动打开软键盘,这样用户就不需要点击短信息字段了。
我想提供一个替代的“按语音搜索”,它将打开对话框,并自动打开“立即说话”窗口。因此,用户无需找到并点按键盘上的“麦克风”按钮。
有什么想法吗?
答案 0 :(得分:4)
是的,有可能。请查看Android SDK中的ApiDemos
示例。有一项名为VoiceRecognition
的活动,它使用RecognizerIntent
。
基本上,你需要做的就是用一些额外的东西来获得正确的意图,然后阅读结果。
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
// identifying your application to the Google service
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
// hint in the dialog
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
// hint to the recognizer about what the user is going to say
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// number of results
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
// recognition language
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"en-US");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
// do whatever you want with the results
}
super.onActivityResult(requestCode, resultCode, data);
}