gpt4 book ai didi

java - CountDownTimer onTick() 无法更新类的成员

转载 作者:行者123 更新时间:2023-12-02 12:49:57 24 4
gpt4 key购买 nike

我在 onTick() 方法中更新了 CountDownTimer 内的字段。但是,该字段始终为 0。您知道是什么导致了问题吗?

public class Counter {

private static final long SECOND_MS = 1000;
private static final long MINUTE_MS = 60 * SECOND_MS;
private static final long TOTAL_TIME_MS = 5 * MINUTE_MS;

public long mSecondsSinceLastPick;

public long getSecondsSinceLastSchedulerStarted() {
Timber.d( String.format("get mSecondsSinceLastPick = %s", mSecondsSinceLastPick) );
return mSecondsSinceLastPick;
}


private CountDownTimer mCountDownTimer = new CountDownTimer(TOTAL_TIME_MS, SECOND_MS) {

public void onTick(long millisUntilFinished) {
Timber.d( String.format("onTick mSecondsSinceLastPick = %s", mSecondsSinceLastPick) );
mSecondsSinceLastPick = (TOTAL_TIME_MS - millisUntilFinished) / SECOND_MS;
}

public void onFinish() {
}
};

public void startCounting() {
mCountDownTimer.start();
}
}

[已编辑]日志输出看起来像

06-19 15:42:57.293 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 4
06-19 15:42:58.293 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 5
06-19 15:42:59.299 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 6
06-19 15:43:00.320 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 7
06-19 15:43:01.321 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 8
06-19 15:43:01.507 9994-9994/com.nav.survey D/GroundTruthReminderManager:22: get mSecondsSinceLastPick = 0
06-19 15:43:02.322 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 9
06-19 15:43:03.324 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 10
06-19 15:43:04.325 9994-9994/com.nav.survey D/GroundTruthReminderManager:38: onTick mSecondsSinceLastPick = 11

最佳答案

问题是你没有启动计时器

 mCountDownTimer.start();

private CountDownTimer mCountDownTimer = new CountDownTimer(TOTAL_TIME_MS, SECOND_MS) {

public void onTick(long millisUntilFinished) {
Timber.d( String.format("onTick mSecondsSinceLastPick = %s", mSecondsSinceLastPick) );
mSecondsSinceLastPick = (TOTAL_TIME_MS - millisUntilFinished) / SECOND_MS;
}

public void onFinish() {
}
}.start();

关于java - CountDownTimer onTick() 无法更新类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com