Android startActivityForResult请求代码与启动Google语音到文本时给出的代码不同

时间:2015-04-10 17:35:11

标签: android speech-recognition

在我的应用中,我正在尝试使用片段中的RecognizerIntent启动Google语音到文本。请求代码的值为1010但在startActivityForResult上返回的请求代码是197618。

请求varable已设置为:

// 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方法,其中intent已经启动:

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

这是我得到的日志:

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

测试中: 三星Galaxy S2, 杰利贝恩

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

3 个答案:

答案 0 :(得分:0)

尝试在onActivityResult()中使用它     res = requestCode & 0xffff; 然后检查&#34; res&#34;像往常一样。

答案 1 :(得分:0)

试试这个:

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

这对我有用!

答案 2 :(得分:0)

对于那些像我一样了解这一点的人-如果您是从Fragment调用UPDATE c SET c.Title_Ok = ct.Title_Ok FROM Categories c INNER JOIN Categories_Title ct ON c.Name_Good = ct.Name; -像这样添加startActivityForResult(intent, REQUEST_CODE_VOICE);

  

getActivity()。startActivityForResult(intent,REQUEST_CODE_VOICE);

没有它,您的getActivity()方法将被各种请求代码触发,除了所需的一种(您在onActivityResult()中使用过的代码)。

希望这会帮助到某人