【发布时间】:2015-04-08 18:11:29
【问题描述】:
我使用CountDownTimer 来实现我在Activity 中的一些倒计时功能。我决定离开CountDownTimer 并使用ScheduledThreadPoolExecutor,因为CountDownTimers 不能在onTick() 中取消自己。
由于某种原因,我在以下代码中的Runnable 只执行一次。我不确定为什么它没有执行多次。 destroyCountdownTimer() 函数未命中。
private ScheduledThreadPoolExecutor mCountdownTimer;
private Tick mTick;
class Tick implements Runnable {
@Override
public void run() {
Log.e("tick", String.valueOf(mAccumulatedMilliseconds));
mAccumulatedMilliseconds += 1000;
populateTimeAccumulated();
populateTimeRemaining();
updatePercentages();
if (mTotalMilliseconds <= mAccumulatedMilliseconds) {
destroyCountdownTimer();
}
}
}
private void startCountdown() {
if (mAccumulatedMilliseconds < mTotalMilliseconds) {
mCounterIsRunning = true;
if (mCountdownTimer == null) {
mCountdownTimer = new ScheduledThreadPoolExecutor(1);
}
if (mTick == null) {
mTick = new Tick();
}
mCountdownTimer.scheduleAtFixedRate(mTick, 1000, 1000, TimeUnit.MILLISECONDS);
}
}
private void destroyCountdownTimer() {
if (mCountdownTimer != null) {
mCountdownTimer.shutdownNow();
mCountdownTimer = null;
}
if (mTick != null) {
mTick = null;
}
}
【问题讨论】:
-
将日志记录添加到您的取消逻辑(或者如果必须,使用断点调试器)并查看是否触发。
-
我在调用中设置了一个断点来销毁计时器。它没有被击中。在注释掉
Runnable中的填充/更新调用后,一切正常。我看不出为什么他们中的任何人都应该持有Runnable。当我使用CountDownTimer时,它们都运行良好。我稍后会用其中的代码更新主帖。