gpt4 book ai didi

java - 如果我暂停计时器,我将面临计时器的问题,它不会从停止的地方开始

转载 作者:行者123 更新时间:2023-12-02 00:58:49 25 4
gpt4 key购买 nike

如果我暂停 chronometer,我会遇到 chronometer 问题,它不会从停止的地方开始。所以我需要从停止的地方开始。在 onlocation 更改方法中,如果米小于 5 米,时间将开始,如果大于 5 米,时间将停止,但不会从停止的地方开始

public class MainActivity{

@Override
public void onLocationChanged(@NonNull Location location) {

if (mPresenter.getLocalSTATUS() != null && mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_CONTINUE_RIDE))
{
if(mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_CONTINUE_RIDE))
{

if (results[0] < 5.09) {
cmTimer.start();

}
else {
pauseOffset = SystemClock.elapsedRealtime();
cmTimer.stop();

}


}
}

}

public void DonePickUpUI_OnClick() {
if (!resume) {
cmTimer.setBase(SystemClock.elapsedRealtime());
cmTimer.start();

} else {
cmTimer.start();
}
assert cmTimer != null;
cmTimer.setOnChronometerTickListener(arg0 -> {
if (!resume) {
if(mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_START_RIDE)) {
long minute = (((SystemClock.elapsedRealtime() - cmTimer.getBase()) / 1000) / 60);
long second = (((SystemClock.elapsedRealtime() - cmTimer.getBase()) / 1000) % 60);
elapsedTime_Done_pickup = SystemClock.elapsedRealtime();
Log.d(TAG, "onChronometerTick: " + minute + " : " + second);
offline_waiting_time.setText(minute + " : " + second);
}
} else {
if (mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_START_RIDE)) {
long minute = ((elapsedTime_Done_pickup - cmTimer.getBase()) / 1000) / 60;
long second = ((elapsedTime_Done_pickup - cmTimer.getBase()) / 1000) % 60;
elapsedTime_Done_pickup = elapsedTime_Done_pickup + 1000;
Log.d(TAG, "onChronometerTick: " + minute + " : " + second);
offline_waiting_time.setText(minute + " : " + second);
/*offline booking*/
}
}
});
/*offline booking*/


}

private void startRide_OnCLICK() {

cmTimer.setOnChronometerTickListener(arg0 -> {
if (!resume) {
if(mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_CONTINUE_RIDE)) {
long minute = (((SystemClock.elapsedRealtime() - cmTimer.getBase()) / 1000) / 60) - elapsedTime_Done_pickup;
long second = (((SystemClock.elapsedRealtime() - cmTimer.getBase()) / 1000) % 60) - elapsedTime_Done_pickup;
elapsedTime_Done_pickup = SystemClock.elapsedRealtime();
Log.d(TAG, "onChronometerTick: " + minute + " : " + second);
offline_waiting_time.setText(minute + " : " + second);
}
} else {
if (mPresenter.getLocalSTATUS().equalsIgnoreCase(LOCAL_CONTINUE_RIDE)) {
long minute = ((elapsedTime_Done_pickup - cmTimer.getBase()) / 1000) / 60;
long second = ((elapsedTime_Done_pickup - cmTimer.getBase()) / 1000) % 60;
elapsedTime_Done_pickup = elapsedTime_Done_pickup + 1000;
Log.d(TAG, "onChronometerTick: " + minute + " : " + second);
offline_waiting_time.setText(minute + " : " + second);
/*offline booking*/
}
}
});



}


}}

最佳答案

您需要一个跟踪时间的变量。

long timeWhenStopped = 0;

停止计时器时更新变量的值

timeWhenStopped = cmTimer.getBase() - SystemClock.elapsedRealtime();
cmTimer.stop();

启动前使用此变量调整计时器

cmTimer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
cmTimer.start();

最后,如果您有办法重置计时器,那么您应该记住还重置 timeWhenStopped 变量。

cmTimer.setBase(SystemClock.elapsedRealtime());
timeWhenStopped = 0;

关于java - 如果我暂停计时器,我将面临计时器的问题,它不会从停止的地方开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786716/

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