I have been trying to code for a countdown timer with CountDownTimer, and make it able to resume from the stop time. So far I was able to refer to the other question and come up with this.
public void onClick(View v) {
getResume();
if ( getResume()==0) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
setResume(1);
Toast.makeText(this, getResume()+"",Toast.LENGTH_LONG).show();
} else if (getResume()==1) {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");
setResume(2);
Toast.makeText(this, getResume()+"",Toast.LENGTH_LONG).show();
} else if (getResume()==2){
CountDownTimer rcdt = new MyCountDownTimer(s1, interval);
rcdt.start();
setResume(1);
}
}
So getResume() is just to get global int resume, which I thought I could use to act as a switch, since timerHasStarted was not helping me very much. However, since I am creating the new timer when resume is 2, I was not able to resume the timer more than once. This code only let me resume once because it is different timer, and after rcdt.start() happens I can't even .cancel(). Clearly I am missing an important concept, but I could not find out by searching. https://gist.github.com/bverc/1492672 unlike this case, I just make the milisuntilfinished = s1, and using it to start new timer. Is this way to go? or is there some way to make the code above work? Please help!
try to replace this line (in your if==2)
CountDownTimer rcdt = new MyCountDownTimer(s1, interval);
with
countDownTimer= new MyCountDownTimer(s1, interval);
let me know if its works and fi you udnerstood what was the problem, if it was the problem but you didnt understood what was wrong I will post explanation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments