倒数计时器不循环

时间:2013-02-15 20:32:33

标签: android countdown

基本上我正在做一个有氧运动功能,并且有三个倒计时器连续嵌套在一起,所以当一个计时器结束时,下一个计时器开始。一个用于准备时间,一个用于锻炼时间,一个用于休息时间,用户选择这些时间。

我需要它循环,但是用户从数字选择器中选择了多次,但无论我做什么,它只通过它一次并且不循环所以我知道它都工作它只是循环部分不工作

我在这里遗漏了什么吗?有更好的方法吗?

    //Main countdown timers loop
    for(int i = 0; i <= times.getValue() + 1; i++) //times NumberPicker
    {
         prepCountTimer = new CountDownTimer(_finalPrep * 1000, 1000) {

             public void onTick(long millisUntilFinished) {

                 tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                 if(millisUntilFinished <= (6 * 1000))
                 {
                     tvCountDown.setTextColor(Color.RED);
                 }
             }

             public void onFinish() {
                 workoutCountTimer = new CountDownTimer(_finalWorkout * 1000, 1000) {

                     public void onTick(long millisUntilFinished) {
                         tvCountDown.setTextColor(Color.GREEN);
                         tvCountDown.setText((millisUntilFinished / 1000) + "s");
                         if(millisUntilFinished <= 6 * 1000)
                         {
                             tvCountDown.setTextColor(Color.RED);
                         }
                     }

                     public void onFinish() {
                         restCountTimer = new CountDownTimer(_finalRest * 1000, 1000) {

                             public void onTick(long millisUntilFinished) {
                                 tvCountDown.setTextColor(Color.GREEN);
                                 tvCountDown.setText((millisUntilFinished / 1000) + "s");
                                 if(millisUntilFinished <= 6 * 1000)
                                 {
                                     tvCountDown.setTextColor(Color.RED);
                                 }
                             }

                             public void onFinish() {
                                 roundCount = roundCount + 1;
                             }
                          }.start();
                     }
                  }.start();
             }
          }.start();

    }

1 个答案:

答案 0 :(得分:0)

这里的问题是你创建prepCountTimer并在完成时分配,然后启动它。然后它到达每个的结尾并再次循环制作另一个preopCountTimer并启动它。一旦完成,您需要让restCountTimer开始下一个preopCountTimer。除非我在这里理解错误。

public void callingMethod() {
    timerMethod(times.getValue());
    // execution continues as your timer will run in a different thread
}

public void timerMethod(final int count) {
    if (count == 0) {
        // we have done the number of timers we want we can
        // call whatever we wanted to once our timers were done
    }
    //you could use count to get the times for each timer here
    startTimer(_finalPrep, new timerListner() {
        @Override
        public void timerFinish() {
            //when timer 1 finishes we will start timer 2
            startTimer(_finalWorkout, new timerListner() {
                @Override
                public void timerFinish() {
                    //when timer 2 finishes we will start timer 3
                    startTimer(_finalRest, new timerListner() {
                        @Override
                        public void timerFinish() {
                            //when timer 3 finishes we want to call the next timer in the list.
                            timerMethod(count - 1);
                        }
                    });
                }
            });
        }
    });
}

private interface timerListner {
    void timerFinish();
}

public void startTimer(int timerTime, final timerListner onFinish) {
    // you can pass in other parameters unqiue to each timer to this method aswell
    CountDownTimer timer = new CountDownTimer(timerTime * 1000, 1000) {
        public void onTick(long millisUntilFinished) {
            tvRoundCount.setText("Round " + roundCount + " / " + times.getValue());
            tvCountDown.setText((millisUntilFinished / 1000) + "s");
            if (millisUntilFinished <= (6 * 1000)) {
                tvCountDown.setTextColor(Color.RED);
            }
        }

        @Override
        public void onFinish() {
            onFinish.timerFinish();
        }
    };
    timer.start();

}