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

Demo 阅读:19 2024-02-27 23:08:18 评论:0

在我的应用程序中,我正在尝试使用 fragment 中的 RecognizerIntent 启动谷歌语音转文本。请求代码的值是 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); 

Fragment 的 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 

测试于: 三星盖乐世 S2, 糖 bean

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

请您参考如下方法:

对于那些和我一样弄清楚这个问题的人 - 如果你从 Fragment 调用 startActivityForResult(intent, REQUEST_CODE_VOICE); - 添加 getActivity()像这样:

getActivity().startActivityForResult(intent, REQUEST_CODE_VOICE);

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

希望对大家有帮助


标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号