nim_10
nim_10

Reputation: 496

Android startActivityForResult request code different than given when starting google voice-to-text

In my app, I am trying to start google voice-to-text using RecognizerIntent from a fragment. The value of the request code was given 1010 but on the startActivityForResult the request code returned was 197618.

The request varable has been set as:

// static result code, random integer
public static final int REQUEST_CODE_VOICE = 1010;

// set intent for recognize speech
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

onActivityCreated method of fragment, where intent has been started:

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // put language
    //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));

    // tap button on click listener
    btnTap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            try {
                // start activity
                Log.d("onActivityResult", "voice activity started");
                startActivityForResult(intent, REQUEST_CODE_VOICE);
                Log.d("onActivityResult", "voice activity finished");
            } catch(ActivityNotFoundException e) {
                // the device does not support android speech
                showToast("Your Device Does Not Support Speech Recognition!");
            }
        }
    });
}

Here is the onActivityResult method at MainActivity

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.d("onActivityResult", "request: "+requestCode+", result: "+resultCode);

    switch(requestCode) {
        // Speech to Text
        case VoiceControllerFragment.REQUEST_CODE_VOICE:
            // get the results
            ArrayList<String> text
                    = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            // show results
            TextView tvRecognizedText = (TextView) findViewById(R.id.tv_recognized_text);
            // tmp string
            //String tmpStr = "";
            tvRecognizedText.setText( "Success: " );

            Log.d("onActivityResult voice", "result: "+RESULT_OK);

            if(resultCode == RESULT_OK && data != null) {
                // concatenate all results
                for(String str : text) {
                    tvRecognizedText.setText( tvRecognizedText.getText().toString() + str );
                }
                Log.d("speech", text.get(0));
            } else {
                tvRecognizedText.setText( "Sorry! Unrecognizable speech.\nTry again!!" );
            }
            break;
    }
}

Here is the log I got:

04-10 23:11:13.005  29915-29915/? D/onActivityResult﹕ voice activity started
04-10 23:11:13.065  29915-29915/? D/onActivityResult﹕ voice activity finished
04-10 23:11:24.855  29915-29915/? D/onActivityResult﹕ request: 197618, result: -1

Tested in: Samsung Galaxy S2, JellyBean

ref: http://viralpatel.net/blogs/android-speech-to-text-api/ http://www.androidhive.info/2014/07/android-speech-to-text-tutorial/

Upvotes: 3

Views: 1667

Answers (3)

Roman T.
Roman T.

Reputation: 393

For those who was figuring out this as well as I was - if you are calling startActivityForResult(intent, REQUEST_CODE_VOICE); from Fragment - add getActivity() to it like so:

getActivity().startActivityForResult(intent, REQUEST_CODE_VOICE);

Without it your onActivityResult() method will be triggered with various request codes, except one needed (the one that you've used in startActivityForResult()).

Hope this'll help someone

Upvotes: 1

Ali Gorji
Ali Gorji

Reputation: 327

Try this:

... getActivity().startActivityForResult(intent, REQUEST_CODE_VOICE); ...

It's work for me!

Upvotes: 0

f0rrest
f0rrest

Reputation: 516

Try use this in your onActivityResult() res = requestCode & 0xffff; Then check "res" as usual.

Upvotes: 0

Related Questions