Reputation: 651
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!
Upvotes: 1
Views: 2629
Reputation: 485
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
Upvotes: 0
Reputation: 4256
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.
Upvotes: 0