Android:在倒数计时器上恢复

时间:2011-12-02 04:27:42

标签: android

我是Java和Android编程的初学者。我对如何恢复倒计时器感到困惑    pause. counter.ontick(milisUntilFinished);在我的代码中不起作用后。
   请帮帮我,我有点急。

这是我的代码:

@Override  
protected void onRestoreInstanceState(Bundle cute) {  
super.onRestoreInstanceState(cute);}  

@Override  
protected void onSaveInstanceState(Bundle outState) {  
super.onSaveInstanceState(outState);}  

Timer.setOnClickListener(TimerClickListener);  
counter = new MyCount(orig, 1000);  
private OnClickListener TimerClickListener = new OnClickListener() {  

public void onClick(View v) {  
if (p1<=4){  
if (decision==0){  
counter.start();  
decision=1;}  
else if(decision==1){  
//pause
counter.cancel();  
decision=2; }  
else{  
//resume  
counter.onTick(orig);  
decision=1;   
}  
//end if  
}   } };      
class MyCount extends CountDownTimer {  
public MyCount(long millisInFuture, long countDownInterval) {  
super(millisInFuture, countDownInterval);  
public void onFinish() {  
//do stuff  
}//end if  
}//finish  
public void onTick(long millisUntilFinished) {  
. . .  
. . .    
orig = millisUntilFinished;  
Timer.setText(String.format("%02d", minutes) + ":"  
+ String.format("%02d", seconds));  
}//class MyCount  

任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:1)

最好使用Chronometer视图。示例为here

答案 1 :(得分:0)

你不能暂停倒数计时器。一旦你使用

counter.cancel();  

你实际上完全取消了计时器。

实际上你可以做的是在你决定恢复后开始一个新的倒数计时器,当然还有新的参数。

答案 2 :(得分:0)

 final CountDownTimer cdt=new CountDownTimer(x,y)
            {
                 public void onTick(long millisUntilFinished){
                 mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                 c= millisUntilFinished;
                 if(cover==6)
                 onFinish();

            }
                 public void onFinish()
                 {                                   
                 Integer i=(Integer) cover*10;
                 String s=i.toString();
                 mTextField.setText("done! "+s);
                 for(int k=1;k<=12;k++)
                       b[k].setClickable(false);

                }            
        }.start();


> define c as long variable assign it to the value of millisUntilFinished and restart the counter with c value as the starting value....

       final CountDownTimer cdt1=new CountDownTimer(c,y)
                        {
                             public void onTick(long millisUntilFinished){
                             mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                             c= millisUntilFinished;
                             if(cover==6)
                             onFinish();
                        }
                             public void onFinish()
                             {                                   
                              Integer i=(Integer) cover*10;
                              String s=i.toString();
                              mTextField.setText("done! "+s);
                             }           
                        }.start();

this code will virtually give u a Countdowntimer with pause and resume control