java – How to make a strict sequence of Timers (switch/case)

Question:

The application loads MediaPlayer , then the Voice recognition , if the voice is equals to the word in the if condition, then that word is put into the TextView and another Voice recognition is loaded. If not equal, then the wrong word is put into the TextView and the MediaPlayer is loaded.

The first time recognition is run, if I answer maserati , then it skips case 1 and jumps straight to case 2 .

How do I make a STRICT recognition sequence so that it starts recognizing with case 1 , which means if I answer maserati , then it should consider it wrong?

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Timer timer1 = new Timer();
    timer1.schedule(new TimerTask() {

        @Override
        public void run() {
            MediaPlayer voice = MediaPlayer.create(MainActivity.this, R.raw.carMercedes);
            voice.start();
            voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer voice) {
                    voice.release();
                    Timer timer = new Timer("1");
                    timer.schedule(new TimerTask() {

                        @Override
                        public void run() {
                            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
                            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
                            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
                            int timerId=Integer.parseInt(Thread.currentThread().getName());
                            startActivityForResult(intent, timerId);
                        }
                    }, 0);
                }
            });
        }
    }, 0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 1:
        if (resultCode == RESULT_OK) {
            ArrayList<String> results;
            results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            TextView speechText = (TextView) findViewById(R.id.textView1);
            String str = "";
            for (int i = 0; i < results.size(); i++) {
                str += results.get(i);
            }
            if (str.equals("mercedes")) {
                rightAnswer();
                speechText.setText(str);
            } else {
                wrongAnswer();
                speechText.setText(str);
            }
        }
    break;
        case 2:
        if (resultCode == RESULT_OK) {
            ArrayList<String> results;
            results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            TextView speechText = (TextView) findViewById(R.id.textView1);
            String str = "";
            for (int i = 0; i < results.size(); i++) {
                str += results.get(i);
            }
        if (str.equals("maserati")) {
            speechText.setText(str);
        } else {
            speechText.setText(str);
        }
    }
    break;
        }
        }

private void wrongAnswer() {
    MediaPlayer voice = MediaPlayer.create(MainActivity.this, R.raw.carMaserati);
    voice.start();
    voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer voice) {
            voice.release();
        }
    });
}
private void rightAnswer() {
    Timer timer = new Timer("2");
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "You may speak!");
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
            int timerId=Integer.parseInt(Thread.currentThread().getName());
            startActivityForResult(intent, timerId);
        }
    }, 0);
}
}

p/s. If you don't know the answer, please upvote the question.

Answer:

You start an Activity for recognition:

int timerId=Integer.parseInt(Thread.currentThread().getName());
startActivityForResult(intent, timerId);

Your thread id is 1 here.

As a result, in onActivityResult you get a unit in requestCode and the first case is triggered. If you, say, do this:

startActivityForResult(intent, 2);

Then the second case will work for you in onActivityResult .

Scroll to Top