gpt4 book ai didi

java - 在 android 的 java 中暂停倒数计时器

转载 作者:行者123 更新时间:2023-11-30 05:02:17 25 4
gpt4 key购买 nike

我正在制作一个计时器,它有一个按钮,我可以控制选择递增模式或倒计时模式。倒计时模式运行良好,开始、暂停、重置。倒数计时器是通过倒数Android studio中的倒数计时器制作的。问题是我无法暂停计数计时器并从它停止的地方恢复。这样做的正确方法是什么?我知道这可以通过计时模式来完成,但我正在尝试制作一个定制的计时器小部件,正如我之前提到的,可以按下一个按钮让用户决定他想要的模式。因此,如果可能的话,我不想在当前设置中使用其他小部件。

public void startBuiltInTimer(int millisecondCountingInterval) {

if (getTimeInMillis() > 0) { //getTimeInMillis will read the time value in the current Timer widget, which is a textView
builtInTimerIsCounting = true; //set the timer is running flag
showAsCounting(true);
if (getTimerCountUpOrDown() == TIMER_COUNT_DOWN) { //countdown mode
countDownTimer = new CountDownTimer(getTimeInMillis(), millisecondCountingInterval) {
@Override
public void onTick(long millisUntilFinished) {
setTime(millisUntilFinished);
}
}

@Override
public void onFinish() {
setTime(0);
builtInTimerIsCounting = false;
showAsCounting(false);
}

}
}.start();
} else if (getTimerCountUpOrDown() == TIMER_COUNT_UP) { //count up mode
long tempTimeInMillis = getTimeInMillis() + lastTimeMillisUntilFinished; //trying to save the last time value in millis and keep the total time value as user defined.
setTime(0); //set initial value of the textView to show the count-up timer start from zero, initially.
countDownTimer = new CountDownTimer(tempTimeInMillis, millisecondCountingInterval) {
@Override
public void onTick(long millisUntilFinished) {
long temp = tempTimeInMillis - millisUntilFinished;
setTime(temp); //reverse the countdown to count-up
lastTimeMillisUntilFinished = millisUntilFinished;//keep the millisUntilFinished for next start.
}

@Override
public void onFinish() {
setTime(tempTimeInMillis);
builtInTimerIsCounting = false;
showAsCounting(false);
}

}
}.start();
}
}

}

编辑:我最终使用 SystemClock 来保持跟踪和记录计数计时器。这是代码。

else if (getTimerCountUpOrDown() == TIMER_COUNT_UP) {
startCountUpTimer(millisecondCountingInterval);
}

public void startCountUpTimer(int millisecInterval) {
if (!countUpTimerIsRunning) {
handler = new Handler();
myRunnable = new MyRunnable(millisecInterval); //MyRunnable can pass a variable thru to let handler have more flexibility with postDelay method. You can still use regular Runnable with hard coded time interval in millisec.
startTime = SystemClock.uptimeMillis(); //use startTime to mark the time when the count up timer start ticking
handler.postDelayed(myRunnable, millisecInterval);
countUpTimerIsRunning = true;
}

}

public class MyRunnable implements Runnable {
private int interval;

public MyRunnable(int t) {
this.interval = t;
}

@Override
public void run() {
millisecondTime = SystemClock.uptimeMillis() - startTime; //this is the time value measured by the count up timer
updateTime = timeBuff + millisecondTime;
setTime(updateTime);
if (updateTime <= countUpMode_Threshold) {
handler.postDelayed(this, interval);
} else {
setTime(countUpMode_Threshold);
handler.removeCallbacks(myRunnable);
builtInTimerIsCounting = false;
showAsCounting(false);
if (onBuiltInTimerStatusChangeListener != null) {
onBuiltInTimerStatusChangeListener.onStatusChange(OnBuiltInTimerStatusChangeListener.STATUS_FINISHED);
}
}
}
}

public void pauseCountUpTimer() {
timeBuff += millisecondTime;
handler.removeCallbacks(myRunnable);
countUpTimerIsRunning = false;
}

最佳答案

一种方法是您应该手动跟踪暂停时间并从该时间再次启动计时器。或者您可以尝试使用位于 this link 的实用程序它具有您正在寻找的功能。很有帮助

关于java - 在 android 的 java 中暂停倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58054760/

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