我正在尝试使用runnable
从30秒倒计时到0,然后运行方法,然后重新启动。我似乎无法正常工作。对于一项简单的任务来说,这似乎是一个非常复杂的解决方案...
tvHeatCD
是TextView
,应该每秒更新一次以显示" 5s"如果还剩5秒等
reduceHeat()
是应该每30秒运行一次的方法(显示的匹配条件)
stopHeatTimer()
只是致电handler.removeCallbacks(..)
和heatTimerRunning = false
public void checkHeatTimer() {
RealmResults<Heat> heatList = realm.where(Heat.class).equalTo("theheat","theheat").findAll();
ht = heatList.get(0);
if(ht.getHeat() !=0) {
if(!heatTimerRunning) {
final int[] countdown = {30};
tvHeatCD.setVisibility(View.VISIBLE);
heatRunnable = new Runnable() {
@Override
public void run() {
heatTimerRunning = true;
if (!realm.isClosed()) {
countdown[0]--;
if(countdown[0] == 0) {
if (ht.getHeat() != 0) {
Crashlytics.setInt("heat", ht.getHeat());
reduceHeat("normal",20);
stopHeatTimer();
checkHeatTimer();
} else {
Crashlytics.setInt("heat", 0);
tvHeatCD.setVisibility(View.GONE);
stopHeatTimer();
}
}
tvHeatCD.setText(countdown[0] + "s");
heatHandler.postDelayed(heatRunnable, 1000);
} else if (realm.isClosed()) {
recreate();
}
}
};
heatHandler.postDelayed(heatRunnable, 30000);
}
} else {
tvHeatCD.setVisibility(View.GONE);
}
}
感谢任何帮助......谢谢!