简体   繁体   English

如何在活动恢复时重置CountDownTimer?

[英]How to reset CountDownTimer on activity resume?

I don't use constant value for my countdown timer, cause it's different time for every next level.So I use timeCount variable set at 150000 for the first level: 我没有为我的倒数计时器使用常数值,导致每个下一级别的时间不同。所以我使用设置在150000的timeCount变量作为第一级:

    long timeCount = 150000;

public String formatTime(long millis) {  
                    String output = "00:00";  
                    long seconds = millis / 1000;  
                    long minutes = seconds / 60;  

                    seconds = seconds % 60;  
                    minutes = minutes % 60;  

                    String sec = String.valueOf(seconds);  
                    String min = String.valueOf(minutes);  

                    if (seconds < 10)  
                        sec = "0" + seconds;  
                    if (minutes < 10)  
                        min= "0" + minutes;  

                    output = min + " : " + sec;  
                    return output;
                }

And my timer: 我的计时器:

public class MyCount extends CountDownTimer {
         public MyCount(long millisInFuture, long countDownInterval) {
             super(millisInFuture, countDownInterval);
         }

         public void onFinish() {


         }
         public void onTick(long millisUntilFinished) {
             vreme.setText("" + millisUntilFinished / 1000);
         }
         }
    // New timer for 40 minutes, starts after initialization
   MyCount brojacVremena = new MyCount(timeCount, 1000) 
   {
       // Updates the text on your "scoreboard" every second
       public void onTick(long millisUntilFinished) 
       {
           vreme.setText("" + formatTime(millisUntilFinished));
       }

       public void onFinish() 
       {
        finish();
       }
   };

So, when the game ends, i present a popup window and the next level is up after OK is pressed. 因此,当游戏结束时,我会弹出一个弹出窗口,按下OK后,下一个级别就会启动。 I call the popup and set new values, for time and some other stuff: 我调用弹出窗口并设置新值,时间和其他一些东西:

Intent i = new Intent(Kviz.this, Popup_nivoi.class);
            if(level==2){
                numberOfQuestions = 13;
                timeCount = 160000;
                greska = 7;
                level++;
}
brojacVremena.cancel();
            startActivityForResult(i, MY_REQUEST2);

But my countdowntimer always starts from the old time, 150000ms. 但我的倒数计时器总是从旧时代开始,即150000毫秒。 How to reset it, cause me changing the timeCount value does not do the job. 如何重置它,导致我更改timeCount值不起作用。 I don't know why. 我不知道为什么。 That variable is changed, I know that for sure, but countdown timer does not use it. 该变量已更改,我知道肯定,但倒数计时器不使用它。

You can not reset time in already created CountDownTimer it is immutable object and your timeCount varibale is used only once when creating this object. 您无法在已创建的CountDownTimer重置时间,它是不可变对象,并且您的timeCount仅在创建此对象时使用一次。 So what you need to do is just create another timer with new time. 所以你需要做的就是用新的时间创建另一个计时器。

UPD: Here is some code UPD:这是一些代码

public class MyCount extends CountDownTimer {
     public MyCount(long millisInFuture, long countDownInterval) {
         super(millisInFuture, countDownInterval);
     }

     public void onFinish() {
         finish();
     }

     public void onTick(long millisUntilFinished) {
         vreme.setText("" + formatTime(millisUntilFinished));
     }
}
MyCount brojacVremena = new MyCount(timeCount, 1000);

Then when you need new timer you have to just write 然后当你需要新的计时器时,你必须写

brojacVremena = new MyCount(timeCount, 1000);

with new timeCount variable. 使用新的timeCount变量。

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

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