0

I'm making a game for android which has a countdown. The countdown works perfectly, but the problem is that when I pause the game (I stop painting the surfaceview), the countdown continues on.

new CountDownTimer(60000, 1000) {
    public void onTick(long millisUntilFinished) {       
        timer = String.valueOf(millisUntilFinished / 1000);
        invalidate();
    }

    public void onFinish() {
    }
}.start();

How can I pause the countdown?

Giacomoni
  • 1,468
  • 13
  • 18
user3013767
  • 137
  • 2
  • 12
  • http://stackoverflow.com/questions/10362014/countdown-timer-with-pause-and-resume – Swedish Architect Mar 03 '14 at 19:25
  • or http://stackoverflow.com/a/9663508/794088 – petey Mar 03 '14 at 19:29
  • A proper solution would be to write your own version of `CountDownTimer` that supports pause & resume. Don't use `TimerTask` since that's creating a background thread and you must not touch the UI from there. – zapl Mar 03 '14 at 19:30

1 Answers1

0

You can do so by making a variable timer

 static int timer = 60000;

Paas this as an argument:

CountdownTimer ct = new CountdownTimer(timer,1000){
     Update your timer variable every time the countdown ticks.
     Timer = timer-1000;
     .
     .

}

Now whenever you want to pause your timer

ct.cancel();

And start it whenever you want by calling

ct.start;
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
Danish Shah
  • 13
  • 1
  • 3