2014-03-03 120 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;