have a question about CountDownTimer. I have to make an application that allows the user to increment the time time by +1, for every click of the button. Then after the button stops being clicked it waits for three seconds, then starts to countdown.
I pasted my code below.
My Issue is: I can't seem to get the Incrementing of the number working correctly, however it seems that after I stop Incrementing the number (onStop()) it directly goes to (onFinish()). Instead of going to the OnTick() and decreasing the number by 1 every second. I have tried numerous ways to fix this, but have been stuck.
Can anyone lead me in the right direction of what to do? Any help would be appreciated. Thank you guys!
public class MainActivity extends Activity {
Button incrementTime, startTime;
public TextView timedisplay;
public myTimer wavetimer;
private long millisInFuture;
private long millisUntilFinished;
private long countDownInterval;
private long onclicktime;
//private WaveInterface model;
public int countdown;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
incrementTime = (Button) findViewById(R.id.button2);
startTime = (Button) findViewById(R.id.button3);
incrementTime.setText("Increment Time");
startTime.setText("Start");
timedisplay = (TextView) findViewById(R.id.mycounter);
timedisplay.setText("Time Left: " + millisUntilFinished);
wavetimer = new myTimer (millisInFuture, 1000);
incrementTime.setOnClickListener(new OnClickListener(){
int countdown = 01;
/**
* On click button Listner
* @return
*/
public void onClick(View v) {
millisInFuture++;
return;
}
});
startTime.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
wavetimer.start();
}
});}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class myTimer extends CountDownTimer {
public long millisInFuture;
private long countDownInterval;
public myTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
millisUntilFinished--;
if (millisUntilFinished == 0){
wavetimer.onFinish();
}
else {
timedisplay.setText("Time Left: " + millisUntilFinished / 1000);
}
}
@Override
public void onFinish() {
timedisplay.setText("Countdown Finished");
wavetimer.cancel();
}
public void onStop() {
millisInFuture = millisInFuture + 1;
timedisplay.setText("Time Left: " + millisInFuture);
}
I can't seem to figure out how to increment the number correctly. I have tried multiple ways and it keeps screwing up. Not sure where to look. Any help would be appreciated. Thank you guys!