问题
I need to to show a countdown timer with 2 time periods, for example i need it to count down a 45 sec, then count down 15 sec and then loop it all over again 4 times.
I tried this code but it seems to start all the timers at ones. I want at to start the first timer and wait till it finish and then start the second timer wait till it finish and then loop it again. what should i do??
for (int i = 0; i < labsNum; i++) {
currLabTV.setText("LAB " + (i + 1));
if(isWorking) {
timer = new WorkCounter(workNum * 1000, 1000);
timer.start();
}
if(!isWorking){
timer = new WorkCounter(restNum * 1000, 1000);
timer.start();
}
}
i extended the CountDownTimer class.
public class WorkCounter extends CountDownTimer {
public WorkCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
String ms = String.format("%02d:%02d" , TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished));
timeTV.setText(ms);
}
@Override
public void onFinish() {
if(isWorking) {
workoutSign.setText("REST");
}else {
workoutSign.setText("WORKOUT");
}
isWorking = !isWorking;
}
}
来源:https://stackoverflow.com/questions/40140197/countdown-timer-loop-android