为什么应用会跳过这行代码中的几个问题?
Why is does the app skip over a few questions in this line of code?
我这里有这段代码,旨在让用户回答一系列问题。为了确保语音输出和输入不会发生冲突,我在每个问题被问到后实施了延迟。但是,现在该应用程序将从第一个问题 "what town are you in?" 直接跳到最后一个问题 "what team did you last play?"。当我尝试调试此代码时,我发现该应用程序实际上确实转到了提示中间问题的语音输入的功能。然而,应用程序只是跳过这些功能,直接跳到最后一个。谁能帮我找出代码中的错误?整个 activity 还有很多代码,所以如果有什么有用的,请告诉我,这样我就可以 post 了。
感谢您的帮助:)
private void promptSpeechInput_town() {
speakWords("Which town are you in?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TOWN);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_win() {
speakWords("Did you win your last game?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_WIN);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_month() {
speakWords("What month is it?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_MONTH);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_day() {
speakWords("What day is it?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_DAY);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_team() {
speakWords("What team did you last play?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TEAM);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q1(){
speakWords("Did you say" + ed23.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q1);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q2(){
speakWords("Did you say" + ed24.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q2);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q3(){
speakWords("Did you say" + ed25.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q3);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q4(){
speakWords("Did you say" + ed26.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q4);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q5(){
speakWords("Did you say" + ed27.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q5);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT_TOWN:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_twn = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed23.setText(result_twn.get(0));
promptSpeechInput_confirm_Q1();
break;
}
}
case REQ_CODE_SPEECH_INPUT_WIN:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_win = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed24.setText(result_win.get(0));
promptSpeechInput_confirm_Q2();
break;
}
}
case REQ_CODE_SPEECH_INPUT_MONTH:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_month = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed25.setText(result_month.get(0));
promptSpeechInput_confirm_Q3();
break;
}
}
case REQ_CODE_SPEECH_INPUT_DAY:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_day = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed26.setText(result_day.get(0));
promptSpeechInput_confirm_Q4();
break;
}
}
case REQ_CODE_SPEECH_INPUT_TEAM:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_team = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed27.setText(result_team.get(0));
promptSpeechInput_confirm_Q5();
break;
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q1:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q1 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q1.get(0).equals("yes")){
promptSpeechInput_win();
}
else if (!(result_confirm_Q1.get(0).equals("yes")) && !(result_confirm_Q1.get(0).equals("no")) ){
promptSpeechInput_confirm_Q1();
}
else if (result_confirm_Q1.get(0).equals("no")){
promptSpeechInput_town();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q2:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q2 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q2.get(0).equals("yes")){
promptSpeechInput_month();
}
else if (!(result_confirm_Q2.get(0).equals("yes")) && !(result_confirm_Q2.get(0).equals("no")) ){
promptSpeechInput_confirm_Q2();
}
else if (result_confirm_Q2.get(0).equals("no")){
promptSpeechInput_win();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q3:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q3 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q3.get(0).equals("yes")){
promptSpeechInput_day();
}
else if (!(result_confirm_Q3.get(0).equals("yes")) && !(result_confirm_Q3.get(0).equals("no")) ){
promptSpeechInput_confirm_Q3();
}
else if (result_confirm_Q3.get(0).equals("no")){
promptSpeechInput_month();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q4:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q4 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q4.get(0).equals("yes")){
promptSpeechInput_team();
}
else if (!(result_confirm_Q4.get(0).equals("yes")) && !(result_confirm_Q4.get(0).equals("no")) ){
promptSpeechInput_confirm_Q4();
}
else if (result_confirm_Q4.get(0).equals("no")){
promptSpeechInput_day();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q5:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q5 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q5.get(0).equals("yes")){
movepage();
}
else if (!(result_confirm_Q5.get(0).equals("yes")) && !(result_confirm_Q5.get(0).equals("no")) ){
promptSpeechInput_confirm_Q5();
}
else if (result_confirm_Q5.get(0).equals("no")){
promptSpeechInput_team();
}
}
}
}
}
编辑:我应该注意,在调试时,应用程序实际上不会提示对中间问题的响应。它只是提出问题并继续下一个问题。
编辑:我在单独的 activity 中再次使用了这段代码,所以我知道现在问题出在代码本身上。
此代码不起作用的原因是每个确认问题末尾没有中断。在每个带有 yes 和 nos 的 if 语句的末尾添加一个换行符。
我这里有这段代码,旨在让用户回答一系列问题。为了确保语音输出和输入不会发生冲突,我在每个问题被问到后实施了延迟。但是,现在该应用程序将从第一个问题 "what town are you in?" 直接跳到最后一个问题 "what team did you last play?"。当我尝试调试此代码时,我发现该应用程序实际上确实转到了提示中间问题的语音输入的功能。然而,应用程序只是跳过这些功能,直接跳到最后一个。谁能帮我找出代码中的错误?整个 activity 还有很多代码,所以如果有什么有用的,请告诉我,这样我就可以 post 了。
感谢您的帮助:)
private void promptSpeechInput_town() {
speakWords("Which town are you in?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TOWN);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_win() {
speakWords("Did you win your last game?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_WIN);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_month() {
speakWords("What month is it?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_MONTH);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_day() {
speakWords("What day is it?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_DAY);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_team() {
speakWords("What team did you last play?");
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_TEAM);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q1(){
speakWords("Did you say" + ed23.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q1);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q2(){
speakWords("Did you say" + ed24.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q2);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q3(){
speakWords("Did you say" + ed25.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q3);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q4(){
speakWords("Did you say" + ed26.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q4);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
private void promptSpeechInput_confirm_Q5(){
speakWords("Did you say" + ed27.getText().toString());
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
Intent STSintent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
STSintent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
STSintent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
try {
startActivityForResult(STSintent, REQ_CODE_SPEECH_INPUT_CONFIRM_Q5);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
}, 2000);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT_TOWN:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_twn = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed23.setText(result_twn.get(0));
promptSpeechInput_confirm_Q1();
break;
}
}
case REQ_CODE_SPEECH_INPUT_WIN:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_win = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed24.setText(result_win.get(0));
promptSpeechInput_confirm_Q2();
break;
}
}
case REQ_CODE_SPEECH_INPUT_MONTH:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_month = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed25.setText(result_month.get(0));
promptSpeechInput_confirm_Q3();
break;
}
}
case REQ_CODE_SPEECH_INPUT_DAY:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_day = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed26.setText(result_day.get(0));
promptSpeechInput_confirm_Q4();
break;
}
}
case REQ_CODE_SPEECH_INPUT_TEAM:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_team = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
ed27.setText(result_team.get(0));
promptSpeechInput_confirm_Q5();
break;
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q1:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q1 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q1.get(0).equals("yes")){
promptSpeechInput_win();
}
else if (!(result_confirm_Q1.get(0).equals("yes")) && !(result_confirm_Q1.get(0).equals("no")) ){
promptSpeechInput_confirm_Q1();
}
else if (result_confirm_Q1.get(0).equals("no")){
promptSpeechInput_town();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q2:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q2 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q2.get(0).equals("yes")){
promptSpeechInput_month();
}
else if (!(result_confirm_Q2.get(0).equals("yes")) && !(result_confirm_Q2.get(0).equals("no")) ){
promptSpeechInput_confirm_Q2();
}
else if (result_confirm_Q2.get(0).equals("no")){
promptSpeechInput_win();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q3:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q3 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q3.get(0).equals("yes")){
promptSpeechInput_day();
}
else if (!(result_confirm_Q3.get(0).equals("yes")) && !(result_confirm_Q3.get(0).equals("no")) ){
promptSpeechInput_confirm_Q3();
}
else if (result_confirm_Q3.get(0).equals("no")){
promptSpeechInput_month();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q4:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q4 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q4.get(0).equals("yes")){
promptSpeechInput_team();
}
else if (!(result_confirm_Q4.get(0).equals("yes")) && !(result_confirm_Q4.get(0).equals("no")) ){
promptSpeechInput_confirm_Q4();
}
else if (result_confirm_Q4.get(0).equals("no")){
promptSpeechInput_day();
}
}
}
case REQ_CODE_SPEECH_INPUT_CONFIRM_Q5:{
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result_confirm_Q5 = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if(result_confirm_Q5.get(0).equals("yes")){
movepage();
}
else if (!(result_confirm_Q5.get(0).equals("yes")) && !(result_confirm_Q5.get(0).equals("no")) ){
promptSpeechInput_confirm_Q5();
}
else if (result_confirm_Q5.get(0).equals("no")){
promptSpeechInput_team();
}
}
}
}
}
编辑:我应该注意,在调试时,应用程序实际上不会提示对中间问题的响应。它只是提出问题并继续下一个问题。
编辑:我在单独的 activity 中再次使用了这段代码,所以我知道现在问题出在代码本身上。
此代码不起作用的原因是每个确认问题末尾没有中断。在每个带有 yes 和 nos 的 if 语句的末尾添加一个换行符。