Android 语音识别无法从片段中工作

Android 语音识别无法从片段中工作,android,android-fragments,speech-recognition,Android,Android Fragments,Speech Recognition,我有一个应用程序,使用动作识别语音意图。当我从一个活动调用startActivityForResult并且相应的onActivityResult在同一个活动中时,它可以正常工作 我的问题是我有另一个应用程序使用片段。如果我从片段调用startActivityForResult,则不会出现语音框 我所尝试的: 我已尝试在父活动中重写onActivityResult,然后调用getActivity.startActivityForResult(意图、语音请求代码); 这只会启动intent,并执行A

我有一个应用程序,使用动作识别语音意图。当我从一个活动调用startActivityForResult并且相应的onActivityResult在同一个活动中时,它可以正常工作

我的问题是我有另一个应用程序使用片段。如果我从片段调用startActivityForResult,则不会出现语音框

我所尝试的:

我已尝试在父活动中重写onActivityResult,然后调用getActivity.startActivityForResult(意图、语音请求代码); 这只会启动intent,并执行Activity类中的onActivityForResult

我试过以下文章中的各种组合

这是我目前拥有的代码和日志。有人有什么建议吗

注意:我在清单中也有以下权限:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET" />

片段类:

private static final int SPEECH_REQUEST_CODE = 0;

    // Create an intent that can start the Speech Recognizer activity
    private void displaySpeechRecognizer() {
        Log.e(TAG, "inside displaySpeechRecognizer() and speechRequestCode = " + SPEECH_REQUEST_CODE);
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    }

    // This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.e(TAG, "inside onActivityForResult in child fragment. requestCode = " + requestCode + " resultCode = " + resultCode);

        if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
            List<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            String spokenText = results.get(0);
            // Do something with spokenText
            Log.e(TAG, "spokenText = " + spokenText);
            etCommentEditText.append(" " + spokenText);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

您必须调用
recognizer.startListening(speechIntent)和用于停止
识别器。停止侦听()

现在它在片段中工作


假设,当点击按钮调用上述方法时,您想初始化语音识别器。

嘿,这很有效,谢谢。我在网上没有找到任何关于这种方法的教程。一个问题是,如何让本机对话框出现,告诉用户spech识别实际上正在发生?另一种方法是在中间有一个麦克风的盒子,只需创建一个自定义对话框。下面是我遵循的XML代码。在dialog.show()之后;呼叫识别器。惊人监听(speechIntent);然后在关闭对话框后调用recognizer.stopListening();啊,好吧,我想可能有一种我可以称之为土著的东西。好的,谢谢你的帮助
private static final int SPEECH_REQUEST_CODE = 0;

    // Create an intent that can start the Speech Recognizer activity
    private void displaySpeechRecognizer() {
        Log.e(TAG, "inside displaySpeechRecognizer() and speechRequestCode = " + SPEECH_REQUEST_CODE);
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
        startActivityForResult(intent, SPEECH_REQUEST_CODE);
    }

    // This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        Log.e(TAG, "inside onActivityForResult in child fragment. requestCode = " + requestCode + " resultCode = " + resultCode);

        if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
            List<String> results = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            String spokenText = results.get(0);
            // Do something with spokenText
            Log.e(TAG, "spokenText = " + spokenText);
            etCommentEditText.append(" " + spokenText);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    05-02 14:57:37.012 14414-14414/com.carefreegroup.rr3.carefreeoncall E/AlertDetailsFragment: inside displaySpeechRecognizer() and speechRequestCode = 0
05-02 14:57:37.072 14414-14414/com.carefreegroup.rr3.carefreeoncall E/AlertDetailsFragment: inside onActivityForResult in child fragment. requestCode = 0 resultCode = 0