gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:45:44 28 4
gpt4 key购买 nike

在我的应用程序中,我正在尝试使用 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 startActivityForResult 请求代码与启动谷歌语音到文本时给出的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29567457/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com