gpt4 book ai didi

java - android countdowntimer 刻度不准确

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:55 25 4
gpt4 key购买 nike

我正在使用倒数计时器进行音频通知...从一开始它就不准确...

使用初始参数

private final long startCountDown; 
private final long intervalCountDown;
...
startCountDown = 180 * 1000; // 3 mns - to be set from Preferences later
intervalCountDown = 60 * 1000; // 1 mns - to be set from Preferences later
...
public void onTick(long millisUntilFinished) {
Log.d(TAG, "notify countDown: " + millisUntilFinished + " msecs");
}


countDownTimer = new SwimCountDownTimer(startCountDown,intervalCountDown);
....

public void startCountDown() {
Log.d(TAG, "start countDown for " + startCountDown + " msecs" );
countDownTimer.start();
}

我可以在日志中看到初始倒计时正确设置为 180000,但下一个应该是 120000,它设置为 119945 !!!

04-27 14:50:42.146: I/SWIMMER(8670): notify countDown: 180000 msecs
04-27 14:51:42.206: I/SWIMMER(8670): notify countDown: 119945 msecs

这很烦人,因为音频通知器只希望说“2 分钟”而不是“1 分五十九秒”……;为什么间隔不对……?我可以在将文本设置为语音字符串时使用它...但是有什么方法可以获取正确的数据吗?

谢谢你的建议

最佳答案

我知道这是一个老问题 - 但我也遇到过这个问题,我想我会分享我的解决方案。

显然 CountDownTimer 不是很准确,所以我决定使用 java.util.Timer 实现一个更精确的倒数计时器:

public abstract class PreciseCountdown extends Timer {
private long totalTime, interval, delay;
private TimerTask task;
private long startTime = -1;
private boolean restart = false, wasCancelled = false, wasStarted = false;

public PreciseCountdown(long totalTime, long interval) {
this(totalTime, interval, 0);
}

public PreciseCountdown(long totalTime, long interval, long delay) {
super("PreciseCountdown", true);
this.delay = delay;
this.interval = interval;
this.totalTime = totalTime;
this.task = getTask(totalTime);
}

public void start() {
wasStarted = true;
this.scheduleAtFixedRate(task, delay, interval);
}

public void restart() {
if(!wasStarted) {
start();
}
else if(wasCancelled) {
wasCancelled = false;
this.task = getTask(totalTime);
start();
}
else{
this.restart = true;
}
}

public void stop() {
this.wasCancelled = true;
this.task.cancel();
}

// Call this when there's no further use for this timer
public void dispose(){
cancel();
purge();
}

private TimerTask getTask(final long totalTime) {
return new TimerTask() {

@Override
public void run() {
long timeLeft;
if (startTime < 0 || restart) {
startTime = scheduledExecutionTime();
timeLeft = totalTime;
restart = false;
} else {
timeLeft = totalTime - (scheduledExecutionTime() - startTime);

if (timeLeft <= 0) {
this.cancel();
startTime = -1;
onFinished();
return;
}
}

onTick(timeLeft);
}
};
}

public abstract void onTick(long timeLeft);
public abstract void onFinished();
}

用法示例:

this.countDown = new PreciseCountdown(totalTime, interval, delay) {
@Override
public void onTick(long timeLeft) {
// update..
// note that this runs on a different thread, so to update any GUI components you need to use Activity.runOnUiThread()
}

@Override
public void onFinished() {
onTick(0); // when the timer finishes onTick isn't called
// count down is finished
}
};

要开始倒计时,只需调用 countDown.start()。countDown.stop() 停止倒计时,可以使用 countDown.restart() 重新开始。

希望这对以后的任何人都有帮助。

关于java - android countdowntimer 刻度不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23323823/

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