gpt4 book ai didi

android - 更改计时器计划 scheduleAtFixedRate

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:07 26 4
gpt4 key购买 nike

我有 TextView 来频繁更新时间,工作正常,但问题是我想根据不同的条件更改计划时间,但我无法更改计划时间。它继续在最初设置的时间。

private Long mPeriod = Long.parseLong("8000");

public void startTimer() {
Logger.i("startTimer", "startTimer");

stopTimer();
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
// postInvalidate();
Logger.i("timer", "timer");
Thread th = new Thread(new Runnable() {

@Override
public void run() {
Logger.i("thread", "thread");

Long ll = System.currentTimeMillis();

mRelativeTime = DateUtils.getRelativeTimeSpanString(
Long.parseLong(mText + ""), ll, 0);

Long diff = ll - Long.parseLong(mText + "");
// long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 60;

Logger.d("diffMinutes", diffMinutes + "");
Logger.d("diffHours", diffHours + "");
if (diffMinutes == 0 && diffHours == 0) {
Logger.i("5000", "5000");
mPeriod = Long.parseLong("5000");
} else if (diffMinutes < 60 && diffMinutes != 0
&& diffHours == 0) {
Logger.i("30000", "30000");
mPeriod = Long.parseLong("30000");
} else if (diffHours > 0) {
Logger.i("600000", "600000");
mPeriod = Long.parseLong("600000");
}
**// This line is not working...
mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);**
// Change text
handler.sendEmptyMessage(0);

}
});
th.run();
}
};
mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);
}

public void stopTimer() {

if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}

}

最佳答案

您必须创建自己的从 TimerTask 扩展的 CustomTimerTask 类。在此之后,您可以使用 mCustomTimerTask(CustomTimerTask 的对象)重新安排您的时间。 Here是可能重复的答案。

关于android - 更改计时器计划 scheduleAtFixedRate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16968935/

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