Android startActivityForResult请求代码与启动google voice to text时给出的代码不同

Android startActivityForResult请求代码与启动google voice to text时给出的代码不同,android,speech-recognition,Android,Speech Recognition,在我的应用程序中,我正试图从一个片段中使用ReconfizerIntent启动谷歌语音到文本。请求代码的值为1010,但在startActivityForResult上,返回的请求代码为197618 请求变量已设置为: // static result code, random integer public static final int REQUEST_CODE_VOICE = 1010; // set intent for recognize speech Intent intent =

在我的应用程序中,我正试图从一个片段中使用ReconfizerIntent启动谷歌语音到文本。请求代码的值为1010,但在startActivityForResult上,返回的请求代码为197618

请求变量已设置为:

// 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方法的片段,其中意图已启动:

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!");
            }
        }
    });
}
以下是MainActivity中的onActivityResult方法

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;
    }
}
测试地点: 三星Galaxy S2, 水母

参考:

尝试在onActivityResult()中使用此选项
res=requestCode&0xffff
然后像往常一样勾选“res”。

试试这个:

。。。
getActivity().startActivityForResult(意图、请求、代码、语音);
…


这是我的工作

对于那些和我一样明白这一点的人-如果您正在呼叫
startActivityForResult(意图、请求、代码、声音)来自片段-向其添加
getActivity()
,如下所示:

getActivity().startActivityForResult(意图、请求、代码、语音)

如果没有它,您的
onActivityResult()
方法将使用各种请求代码触发,但需要的请求代码除外(您在
startActivityForResult()中使用的代码)

希望这能帮助别人

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