0

I have an app that uses the ACTION_RECOGNIZE_SPEECH intent. It works fine when i call startActivityForResult from an Activity and the corresponding onActivityResult is in the same Activity.

My problem is i have another app that uses fragments. If i call startActivityForResult from the Fragment, the speech box does not appear.

What i have tried:

I've tried overridding onActivityResult in the parent Activity and then call getActivity.startActivityForResult(intent, SPEECH_REQUEST_CODE); This just launches the intent and the onActivityForResult in the Activity class executes.

i've tried various combinations from the following post.

onActivityResult is not being called in Fragment

This is the code i have at the moment and the logs. Has anyone any suggestions?

NB i have the following permissions in the manifest too:

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

.

Parent Activity:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.e(TAG, "inside onActivityForResult in parent. requestCode = " + requestCode + "resultCode = " + requestCode + " data = " + data );
    }

.

Fragment class:

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);
    }

.

Logs:

    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
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
turtleboy
  • 8,210
  • 27
  • 100
  • 199

1 Answers1

1

you must call recognizer.startListening(speechIntent); and for stoping recognizer.stopListening();

Now it works in fragments.

Suppose, you want to initialize speech recognizer when button clicked call above methods.

  • Hey that worked great thanks. I didn't find anything on the net about tutorials for this way of doing it. One question, how do i get the native dialog box to appear that tells the user the spech recogntion is actually happening? The other way of doing it has a box with a microphone in the middle. – turtleboy May 08 '18 at 09:32
  • Just Create a custom dialog. Below is the XML code I followed. – Bhanu Prakash Pasupula May 08 '18 at 10:05
  • after dialog.show(); call recognizer.startListening(speechIntent); and then after closing dialog call recognizer.stopListening(); – Bhanu Prakash Pasupula May 08 '18 at 10:11
  • Ah ok, i thought there may be a native thing i could call. Ok thanks for your help – turtleboy May 08 '18 at 10:30