countdown timer loop android

纵饮孤独 提交于 2021-01-28 19:42:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!