0
我更新onTick()方法中的CountDownTimer內的字段。但是,該字段始終爲0.您是否瞭解導致問題的原因?CountDownTimer onTick()無法更新類的成員
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
我開始了,但忘了複製這段代碼。一般來說,當我將它記錄在onTick()中時,一切都會正確顯示。當我在getter中記錄字段時,它總是0. – qbait
你什麼時候得到它?你只有一次嗎? –
我在計數期間多次打電話給getter。我添加了日誌。 – qbait