1

I am a beginner in Java and Android programming. And I am confused on how to resume my Count Down Timer after the pause. counter.ontick(milisUntilFinished); is not working in my code.
Please help me I am bit rushed.

This is my code:

@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  

Any help would be appreciated!

Vivek Kalkur
  • 2,200
  • 2
  • 21
  • 40
Mineko
  • 641
  • 2
  • 14
  • 21

3 Answers3

1

Better you can use the Chronometer View .Sample is here

Finder
  • 1,217
  • 5
  • 18
  • 46
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
vinoth
  • 485
  • 4
  • 16
0

you cannot pause a countdown timer. Once you use

counter.cancel();  

you actually cancel out the timer altogether.

What you can actually do is to start a new countdown timer once you decide to resume, off course with new parameters.

Umesh
  • 4,406
  • 2
  • 25
  • 37
  • I did what you said but it reset not resume //resume countresume = new MyCount(timeleft, 1000); counter.start(); decision=1; } timeleft should contain the value of the timer as it stops. – Mineko Dec 02 '11 at 05:19
  • when the timer pauses, store the value (desiredTime - timeTickedUntilPaused), and then when you wanto resume the timer...restart a new timer with this stored value. – Umesh Dec 02 '11 at 05:31
  • Sorry Im confused on how to store the variable. I did was to reset rather than resume. thanks for the help! – Mineko Dec 02 '11 at 06:29