I’m building a count down timer app and I let the user press a button to start the timer. When the button is pressed the timer counts down (work session). The user can then wait for the timer to finish or reset the timer by clicking the same button (re-labeled “Reset”).
If they wait for the timer to finish, another timer is started (short time interval – aka break session). At this point, if they press the button (relabeled “End break”) it cancels the break timer and starts another work session.
For some reason when I click my button it won’t start the timer. in my current setup of the code. I have tested the timer and the button and I know they work.
For some reason my onClick method doesn’t start the timer, any help? Do I have to do something to my CountDownTimer class?
public class SimplyPomodoroActivity extends Activity implements OnClickListener {
TextView tvTimer; // used to update timer...
Button btStart; //main button
Vibrator vibrator; // vibrate when button is pressed..
boolean off = true;
boolean working = false;
long longBreak = 8000; // 900000;
long shortBreak = 6000; // 300000;
long workTime = 10000; // 1500000;
long v = 100; // vibration sequence
int pomoCount = 1; // keep track of the number of Pomodoros...
// PomoTimer pomoBreak = new PomoTimer(startTime, interval);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialiaze(); //connect xml to java code and setup listener
}
private void initialiaze() {
tvTimer = (TextView) findViewById(R.id.tvTimer);
btStart = (Button) findViewById(R.id.btStart);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
btStart.setOnClickListener(this); // register listener
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
vibrator.vibrate(v);
//Do stuff
if(off){ //Turn on
//change text
//start work timer --> work timer will go to break automatically
off = false;
working = true;
btStart.setText("Reset");
workCounter.start();
}
if(working){
//turn off
btStart.setText("Start");
workCounter.cancel();
working = false;
off = true;
}else if(!working && !off){
//end break
shortBreakCounter.cancel();
btStart.setText("Reset");
workCounter.start();
}
}
CountDownTimer workCounter = new CountDownTimer(workTime, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
tvTimer.setText("0:00");
working = false;
pomoIncrement();
btStart.setText("End Break");
shortBreakCounter.start();
}
};
CountDownTimer shortBreakCounter = new CountDownTimer(shortBreak, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
working = true;
pomoIncrement();
btStart.setText("Reset");
workCounter.start();
}
};
CountDownTimer longBreakCounter = new CountDownTimer(longBreak, 1000) {
public void onTick(long millisUntilFinished) {
displayRemainingTime(millisUntilFinished);
}
public void onFinish() {
pomoIncrement();
}
};
private void pomoIncrement() {
// increment by one, reset at 8
pomoCount += (pomoCount > 8) ? -pomoCount : 1;
}
private void displayRemainingTime(long millisUntilFinished) {
// TODO Auto-generated method stub
int sec = (int) (millisUntilFinished / 1000) % 60;
int min = (int) ((millisUntilFinished / 1000) / 60);
tvTimer.setText("" + min + ":" + sec);
}
}
My count down timer won’t start in my if(off){…} statement… and when i changed it around to other configurations it won’t cancel my current running CountDownTimer..
Add a
after
so the timer isn’t canceled just after the start.