How can I add numbers into a countdown timer so it counts a different number each time it finishes?
Basically I have made a countdown timer using android, the CountDownTimer class, a TextView and a button. It all works fine.
My problem is that I want my countdown timer to change its time each time one of the countdowns has finished.
For example: The countdown timer starts of by counting down from 5 minutes, now once that countdown has finished I want the countdown to automatically start again but from a different time say 1 minute and it continues like that.
So it should go 5 minutes then 1 minute then say 2 minutes and then 30 seconds then stop. Here is how the time is defined already:
counter = new CountDownTimer(300000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText(formatTime(millisUntilFinished));
}
public void onFinish(){
System.out.println("Finished");
}
};
The 300000 is the milliseconds it counts down from so therefore 5 minutes and the 1000 is the countdown interval, so 1 second.
So now the question remains: are there any ways to make it start counting down from 5 minutes then automatically st开发者_Python百科art a countdown from a minute then 2 minutes then 30 seconds as an example?
I have tried using an array so here is the code I used but the for loop was too fast.
The array:
int[] week1day1 = {10000, 20000, 30000};
The loop:
for (int arrayValue = -1; arrayValue < week1day1.length; arrayValue++) {
maxTime = week1day1[arrayValue];
counter.start();
tv.setText("Pause");
System.out.println("Array Value");
}
In the logcat "Array Value" shows up twice and in the space of about 10 milliseconds. So I think the loop is not waiting for the maxTime to count down and therefore loops straight to the end of the array.
Put this in your program loop:
if(counter.count == 0)
{
switch(counterMax)
{
case 1: counter = new CountDownTimer(maxTime1, countDownInterval1);
case 2: counter = new CountDownTimer(maxTime2, countDownInterval2);
// and so on
}
// put your "timer picker" code here
}
Hope this helps, likely will not work as typed (not looking at the docs), but it should give you a start.
精彩评论