可以打开“开始说话”对话框编程?(Possible to open “Speak Now” dial

2019-09-24 02:51发布

是否有可能打开“现在讲”对话框编程?

目前,如果用户点击我的“搜索”按钮,将打开一个对话框,我有这样的用户并不需要点击文本编辑领域的软键盘自动打开。

我想提供一个替代“语音搜索”,将打开的对话框,并有“请开始说话”窗口自动打开。 因此,用户不必找到并点按键盘上的“话筒”按钮。

有任何想法吗?

Answer 1:

对的,这是可能的。 看看ApiDemos在Android SDK样本。 有一个名为活动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);
}


文章来源: Possible to open “Speak Now” dialog programmatically?