【问题标题】:Android two CountDownTimers executing one after the otherAndroid 两个 CountDownTimers 一个接一个地执行
【发布时间】:2015-09-29 11:34:24
【问题描述】:

我的活动中有两个倒计时。一组从 10 秒开始倒计时,另一组从 5 秒开始倒计时。当我单击倒计时按钮时,计时器 1 启动。当这个计时器结束时,我希望 timer2 启动,问题是 timer2 永远不会被执行。

我有 2 个本地变量来跟踪正在执行的计时器,当我调试时,我看到这些变量的正确值已设置,但第二个计时器仍然没有执行。

这是我的 CountdownTimer 类:

 public class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        if (ctHasStarted) {
            ctHasStarted = false;
            timerValue.setText("00:00:000");

            countDownTimer2.start();
            ct2HasStarted = true;
        }
        if (ct2HasStarted) {
            ct2HasStarted = false;
            timerValue2.setText("00:00:000");

            countDownTimer.start();
            ctHasStarted = true;
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {

        int secs = (int) (millisUntilFinished / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (millisUntilFinished % 1000);

        if (ctHasStarted)
            timerValue.setText(String.format("%02d",mins) + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        if (ct2HasStarted)
            timerValue2.setText(String.format("%02d",mins) + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));

    }
}

ctHasStarted 和 ct2HasStarted 是布尔值,用于跟踪正在运行的计时器。正如我调试时所说,countDownTimer.start() 和 countDownTimer2.start() 都达到了。但是 timer2 的文本字段永远不会更新。甚至行 timerValue2.setText("00:"00:00") 也不起作用。

我在活动的 onCreate 中初始化了两个计时器,例如:

countDownTimer = new MyCountDownTimer(ctStartTime, interval);
countDownTimer2 = new MyCountDownTimer(ct2StartTime, interval);

我注意到的另一件奇怪的事情是,当我将 onTick 事件中的 if (ct2HasStarted) 行更改为 else 时,timerValue2.setText-正在显示倒计时。但是在第二次完成后,尽管在 onFinish 事件中调用了第一次的开始,但第一次没有执行。

我想我错过了什么,但我不知道我错过了什么。有什么建议吗?

【问题讨论】:

    标签: android timer countdowntimer


    【解决方案1】:

    好的,我找到了。我稍微重构了我的代码,但最重要的是在 onFinish 部分添加 return

    我在我的活动中添加了一个新函数,它设置了一些变量。像这样:

     public void startTimer(int counterId){
        CountDownTimer counter = null;
        if (counterId == 1) {
            ctHasStarted = true;
            timerValue2.setText("00:00:000");
            timerValue.setText("00:00:000");
            ct2HasStarted = false;
    
            counter = new MyCountDownTimer(ctStartTime, interval);
        }
        if (counterId == 2) {
            ct2HasStarted = true;
            timerValue2.setText("00:00:000");
            timerValue.setText("00:00:000");
            ctHasStarted = false;
            counter = new MyCountDownTimer(ct2StartTime, interval);
        }
    
        if(counter !=null ){
            counter.start();
        }
    }
    

    我将计数器中的 onFinish 事件更改为:

      @Override
        public void onFinish() {
            if (ctHasStarted) {
                startTimer(2);
                return;
            }
            if (ct2HasStarted) {
                startTimer(1);
                return;
            }
        }
    

    这行得通。 (至少对我来说)。

    【讨论】:

      猜你喜欢
      • 2022-07-04
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 2012-05-10
      • 2020-09-06
      相关资源
      最近更新 更多