在倒计时器中启动另一个计时器

时间:2016-03-15 21:15:04

标签: java android timer

我在圆桌时间结束时遇到暂停问题。当用户点击按钮时,它开始倒计时,当完成的文本返回到我想要的值时。但是当倒计时(计时器)结束时,我希望启动另一个timer2。现在它开始计数了。每个计时器在屏幕上都有自己的textView。 这是代码:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            long roundtime= Long.parseLong(String.valueOf(msg1))*1000; //User set Time for Round
            long pause= Long.parseLong(String.valueOf(msg2))*1000; //User set Time for Pause
            Counting timer = new Counting(roundtime, 1000); // class CountdownTimer
            Counting2 timer2 = new Counting2(pause, 1000); // class CountdownTimer

            timer.start(); // I want to start with this timer first
            timer2.start(); // And when timer is finished start with this in own textView
        }

enter image description here

2 个答案:

答案 0 :(得分:2)

使用CountDownTimer。这是你所需要的。只需启动一个计时器,然后在该计时器的onFinish()方法上启动新计时器。类似的东西:

CountDownTimer timer1 = new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
//do whatever you need here, this gets called every 1000 milliseconds (the 2nd parameter
of the constructor, the first is total time in ms
 }

 public void onFinish() {
    CountDownTimer timer2 = new CountDownTimer(5000, 1000) {
        //same logic
        };
        timer2.start();
    }
};

timer1.start();

答案 1 :(得分:0)

如果我已正确理解您的问题,您需要这样的东西。一旦您的活动开始,必须在&x; xx'之后发生一些事情。秒。然后必须暂停一下' yy'秒,然后同样的事情必须' xx'秒。正确?

您可以使用Handler.postDelayed()

static boolean pause = false;

onCreate(){
    if (pause) {
        delay = xx + yy;
else
       delay = xx;


Handler.postdelay(new ... , delayTime);

然后很明显,在PostDelay的内部类中生成pause = true