Android 两个 CountDownTimer 一个接一个执行
Android two CountDownTimers executing one after the other
我的 activity 中有两个倒数计时器。一组从 10 秒开始倒计时,另一组从 5 秒开始倒计时。当我单击倒计时按钮时,计时器 1 启动。当这个计时器结束时,我希望 timer2 启动,问题是 timer2 永远不会被执行。
我有 2 个本地变量跟踪正在执行的定时器,当我调试时,我看到为这些变量设置了正确的值,但第二个定时器仍然没有执行。
这是我的倒计时-class:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
if (ctHasStarted) {
ctHasStarted = false;
timerValue.setText("00:00:000");
countDownTimer2.start();
ct2HasStarted = true;
}
if (ct2HasStarted) {
ct2HasStarted = false;
timerValue2.setText("00:00:000");
countDownTimer.start();
ctHasStarted = true;
}
}
@Override
public void onTick(long millisUntilFinished) {
int secs = (int) (millisUntilFinished / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (millisUntilFinished % 1000);
if (ctHasStarted)
timerValue.setText(String.format("%02d",mins) + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
if (ct2HasStarted)
timerValue2.setText(String.format("%02d",mins) + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
}
}
ctHasStarted 和 ct2HasStarted 是布尔值,用于跟踪计时器是什么 运行。正如我调试时所说,行 countDownTimer.start() 和 countDownTimer2.start() 都已到达。但是 timer2 的文本字段永远不会更新。即使 timerValue2.setText("00:"00:00") 行也不起作用。
我在 activity 的 onCreate 中初始化了两个计时器,例如:
countDownTimer = new MyCountDownTimer(ctStartTime, interval);
countDownTimer2 = new MyCountDownTimer(ct2StartTime, interval);
我注意到的另一件奇怪的事情是,当我将 onTick 事件中的行 if (ct2HasStarted) 更改为 else timerValue2.setText-显示倒计时。但是第二次结束后,尽管在onFinish事件中调用了第一次的开始,但第一次并没有执行。
我想我错过了什么,但我不知道我错过了什么。有什么建议吗?
好的,我找到了。我稍微重构了我的代码,但最重要的是在 onFinish 部分添加 return。
我在 activity 中添加了一个新函数,它设置了一些变量。像这样:
public void startTimer(int counterId){
CountDownTimer counter = null;
if (counterId == 1) {
ctHasStarted = true;
timerValue2.setText("00:00:000");
timerValue.setText("00:00:000");
ct2HasStarted = false;
counter = new MyCountDownTimer(ctStartTime, interval);
}
if (counterId == 2) {
ct2HasStarted = true;
timerValue2.setText("00:00:000");
timerValue.setText("00:00:000");
ctHasStarted = false;
counter = new MyCountDownTimer(ct2StartTime, interval);
}
if(counter !=null ){
counter.start();
}
}
然后我将柜台中的 onFinish 事件更改为:
@Override
public void onFinish() {
if (ctHasStarted) {
startTimer(2);
return;
}
if (ct2HasStarted) {
startTimer(1);
return;
}
}
行得通。 (至少对我而言)。
我的 activity 中有两个倒数计时器。一组从 10 秒开始倒计时,另一组从 5 秒开始倒计时。当我单击倒计时按钮时,计时器 1 启动。当这个计时器结束时,我希望 timer2 启动,问题是 timer2 永远不会被执行。
我有 2 个本地变量跟踪正在执行的定时器,当我调试时,我看到为这些变量设置了正确的值,但第二个定时器仍然没有执行。
这是我的倒计时-class:
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
if (ctHasStarted) {
ctHasStarted = false;
timerValue.setText("00:00:000");
countDownTimer2.start();
ct2HasStarted = true;
}
if (ct2HasStarted) {
ct2HasStarted = false;
timerValue2.setText("00:00:000");
countDownTimer.start();
ctHasStarted = true;
}
}
@Override
public void onTick(long millisUntilFinished) {
int secs = (int) (millisUntilFinished / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (millisUntilFinished % 1000);
if (ctHasStarted)
timerValue.setText(String.format("%02d",mins) + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
if (ct2HasStarted)
timerValue2.setText(String.format("%02d",mins) + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
}
}
ctHasStarted 和 ct2HasStarted 是布尔值,用于跟踪计时器是什么 运行。正如我调试时所说,行 countDownTimer.start() 和 countDownTimer2.start() 都已到达。但是 timer2 的文本字段永远不会更新。即使 timerValue2.setText("00:"00:00") 行也不起作用。
我在 activity 的 onCreate 中初始化了两个计时器,例如:
countDownTimer = new MyCountDownTimer(ctStartTime, interval);
countDownTimer2 = new MyCountDownTimer(ct2StartTime, interval);
我注意到的另一件奇怪的事情是,当我将 onTick 事件中的行 if (ct2HasStarted) 更改为 else timerValue2.setText-显示倒计时。但是第二次结束后,尽管在onFinish事件中调用了第一次的开始,但第一次并没有执行。
我想我错过了什么,但我不知道我错过了什么。有什么建议吗?
好的,我找到了。我稍微重构了我的代码,但最重要的是在 onFinish 部分添加 return。
我在 activity 中添加了一个新函数,它设置了一些变量。像这样:
public void startTimer(int counterId){
CountDownTimer counter = null;
if (counterId == 1) {
ctHasStarted = true;
timerValue2.setText("00:00:000");
timerValue.setText("00:00:000");
ct2HasStarted = false;
counter = new MyCountDownTimer(ctStartTime, interval);
}
if (counterId == 2) {
ct2HasStarted = true;
timerValue2.setText("00:00:000");
timerValue.setText("00:00:000");
ctHasStarted = false;
counter = new MyCountDownTimer(ct2StartTime, interval);
}
if(counter !=null ){
counter.start();
}
}
然后我将柜台中的 onFinish 事件更改为:
@Override
public void onFinish() {
if (ctHasStarted) {
startTimer(2);
return;
}
if (ct2HasStarted) {
startTimer(1);
return;
}
}
行得通。 (至少对我而言)。