Android识别中的请求码是什么API

What is the request code in Android recognition API

            Intent recIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            recIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                    RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            recIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
            recIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Need to speak");
            try {
                startActivityForResult(intentRec, REQ_CODE);
            } catch (ActivityNotFoundException a) {
                Toast t = Toast.makeText(getApplicationContext(),
                        "Opps! Your device doesn't support Speech to Text",
                        Toast.LENGTH_SHORT);
                t.show();
            }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(resultCode) {
        case REQ_CODE:
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                Toast.makeText(getApplicationContext(), String.valueOf(data.hasExtra(RecognizerIntent.EXTRA_RESULTS)), Toast.LENGTH_SHORT);
            }
        break;
    }
}

我一直在学习识别教程API,但我仍然不明白请求代码是什么以及为什么代码不再有效,它没有显示任何错误甚至没有响应我的行为是假的 那么我该如何使用请求代码呢?代码没有显示任何输出。

您需要对语音到文本进行初始设置,并且每当您尝试说话时都需要调用 speak 方法。 按照以下代码:

    private var mTextToSpeechListener: TextToSpeech? = null
    val REQUEST_CODE_SPEECH_INPUT = 5003

 /**
 * Showing google speech input dialog
 */
private fun speechToTextOutput() {
    val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
    intent.putExtra(
        RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
    )
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say something")
    try {
        startActivityForResult(intent, REQUEST_CODE_SPEECH_INPUT)
    } catch (exp: ActivityNotFoundException) {
        Lg.d(TAG, "speechToTextOutput: $exp")
    }

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == Activity.RESULT_OK) {
        when (requestCode) {
            REQUEST_CODE_SPEECH_INPUT -> {
                val resultSpeech = data!!
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
                if (resultSpeech != null && resultSpeech.size > 0) {
                    mBinding.txtSpeechToText.text = resultSpeech.get(0)
                }
            }
        }
    }
}