简体   繁体   中英

How to pause and resume countdown timer?

I have a countdown timer in my game activity and I need it paused when I get incoming call or something that lose the focus from my activity. I tried like this but it does not work (the onPause method works, it stops the timer, but it wont resume it):

private long total = 180000;

MyCount brojacVremena = new MyCount(total, 1000);

public class MyCount extends CountDownTimer {

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

        @Override
        public void onFinish() {


        @Override
        public void onTick(long millisUntilFinished) {
            total = millisUntilFinished;
            vreme.setText("" + millisUntilFinished / 1000);

        }
        }

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MyCount brojacVremena = new MyCount(total, 1000);
        brojacVremena.start();
    }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    brojacVremena.cancel();
}

I tried even this in my onPause:

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MyCount brojacVremenaNew = new MyCount(total, 1000);
        brojacVremenaNew.cancel();
}

I do not think that you need to extend it, unless really necessary. I second what codeMagic said - store time time left in a variable in onPause() and DO NOT forget to save it in onSaveInstanceState() or to SharedPreference .

When your activity resumes, either due to config change or whatever, you can recreate your timer:

new CountDownTimer(millisUntilFinished, tickTime) {

     public void onTick(long millisUntilFinished) {
         millisUntilFinished -= tickTime;
         // do you wanna know for whom the bell tolls?
     }

     public void onFinish() {
         // it tolls for thee
         millisUntilFinished = 0;
     }
  }.start();  

In onPause() , cancel this timer and in onResume() create a new instance of the timer based on how much time is left :)

EDIT: Something like this:

@Override
public void onResume(){
    super.onResume();
    millisUntilFinished = getMillisFromSharedPreference(KEY);
    if(millisUntilFinished != 0){
        new CountDownTimer(millisUntilFinished, tickTime) {

         public void onTick(long millisUntilFinished) {
             millisUntilFinished -= tickTime;
             // do you wanna know for whom the bell tolls?
         }

         public void onFinish() {
             // it tolls for thee
             millisUntilFinished = 0;
         }
        }.start();
    }
}

@Override
public void onPause(){
    super.onPause();
    saveMillisToSharedPreference(KEY,millisUntilFinished);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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