简体   繁体   English

更改倒数计时器的长度

[英]Change length of Countdowntimer

I want to change the length of my Countdowntimer when it finishs. 我想在结束时更改Countdowntimer的长度。 So I got a Random Number which changes everytime. 所以我得到了一个随时间变化的随机数。

standby_time    = 15000
standby_counter = new CountDownTimer(standby_time,1000) {

            @Override
            public void onTick(long millisUntilFinished) {

                ++standby_zaehler;

            }

            @Override
            public void onFinish() {

                ...
                standby_zaehler = 0;

                rndm_groesse = 60000;
                random_zahl = r.nextInt(rndm_groesse);              
                standby_time = random_zahl;

                standby_counter.start();
            }
        };

Where is my fault ? 我的错在哪里? The time is not changing and takes the first value (15sec) 时间没有改变,取第一个值(15秒)

The countdown timers time isnt ment to be changed. 倒数计时器的时间要更改。 You can recreate the timer to alter the time but because you are looking to start it again inside the onFinish() method it would be wise to use a different implementation better suited for this task. 您可以重新创建计时器以更改时间,但是由于您希望在onFinish()方法中重新启动计时器,因此最好使用更适合此任务的其他实现。 The java.util.Timer class does this well. java.util.Timer类可以很好地做到这一点。

private Timer timer = new Timer();
private int standby_zaehler = 0;
private Random r = new Random();

public void scheduleNextTimer() {
    // reset our countdown
    standby_zaehler = 0;
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            // 1 second passed, remove it to the countdown
            --standby_zaehler;
            // we are finished counting down. get a new max seconds
            if(standby_zaehler < 0) {
                standby_zaehler = r.nextInt(60);
            }
        }
    }, 1000, 1000);
}

As you can see we dont even need to create a new timer, just reset the number and begin again. 如您所见,我们甚至不需要创建新的计时器,只需重置数字并重新开始即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM