ここでは、ユーザーに一連の質問に答えさせることを目的としています。音声出力と入力が衝突しないように、私は各質問の後に遅れをとった。しかし、今、アプリは最初の質問からまっすぐにスキップします "あなたはどんな町ですか?"最後の質問に "どのチームが最後にプレーしたのですか?"私はこのコードをデバッグしようとしたとき、私は実際にアプリケーションが中間の質問の音声入力を促す機能に行くことを発見しました。しかし、アプリは単にこれらの機能をスキップし、最後の機能にまっすぐジャンプします。私のコードで誰かが私のバグを見つけるのを助けることができますか?このアクティビティ全体にはさらに多くのコードがあります。役立つものがあればお知らせください。投稿することができます。助けのためのこのコード行でアプリがいくつかの質問をスキップするのはなぜですか?
感謝:)
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();
}
}
}
}
}
編集:私はデバッグ時、アプリが実際に真ん中の質問に対する応答を要求しないことに注意してください。単に質問をして次の質問に移動するだけです。
編集:別のアクティビティでこのコードを再度使用しました。問題はコードそのものです。