2014-03-03 118 views
0

我正在做一个倒计时的Android游戏。倒数计时完美,但问题是,当我暂停游戏时(我停止绘制表面视图),倒计时继续。暂停CountDown计时器Android

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

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

如何暂停倒计时?

+0

http://stackoverflow.com/questions/10362014/countdown-timer-with-pause-and-resume –

+0

或http://stackoverflow.com/a/9663508/794088 – petey

+0

一个合适的解决方案是编写你自己的支持暂停和恢复的'CountDownTimer'版本。不要使用'TimerTask',因为这是创建后台线程,你不能从那里触摸UI。 – zapl

回答

0

您可以通过一个可变计时器

static int timer = 60000; 

PAAS做到这作为一个参数:

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

} 

现在,只要你想暂停计时器

ct.cancel(); 

开始它w henever你想通过调用

ct.start;