0

I'm working as a football (soccer) referee in Israel and I was asked to write an application that simulates our fitness test for the upcoming season. The test is an interval test, and the user can enter how much time does he run, walk and for how many sets. There is a beep sound for each time you should start/stop running (beep variable is of type MediaPlayer). The chronometer should reset each time you finish running / walking.

The following code almost works - The beep sounds are heard in the right time and stop after the right number of sets, but the screen gets stuck right after the chronometer starts...

I would really appreciate your kind help! Thanks, Yaad

private void testLoop() {
    int i = 0;
    boolean flag = true; //true = running, false = walking
    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.start();

    //run, walk, rep = integers that are set by user input

    beep.start();
    tvRunWalk.setText("Running");
    tvRepNum.setText(String.format("Repetition Number: %d", i + 1));
    while (i < rep) //rep = number of repetitions
    {
        if (SystemClock.elapsedRealtime() - chronometer.getBase() == run * 1000 && flag) //if running time is over and you are now running
        {
            chronometer.setBase(SystemClock.elapsedRealtime());
            flag = false;
            tvRunWalk.setText("Walking");
            beep.start();
        }
        else if (SystemClock.elapsedRealtime() - chronometer.getBase() == walk * 1000 && !flag) //if walking time is over and you are now walking
        {
            chronometer.setBase(SystemClock.elapsedRealtime());
            flag = true;
            i++;
            tvRunWalk.setText("Running");
            tvRepNum.setText(String.format("Repetition Number: %d", i + 1));
            beep.start();
        }
    }
}

1 Answers1

0

Your while loop is blocking the UI. You better use AsyncTask and put your loop in its doInBackground() method in order to properly update the UI.

More info here: http://developer.android.com/reference/android/os/AsyncTask.html

MillaresRoo
  • 3,808
  • 1
  • 31
  • 37