gpt4 book ai didi

android - 在运行计时器时延迟时间

转载 作者:行者123 更新时间:2023-11-29 22:35:32 25 4
gpt4 key购买 nike

我有一个 Android 应用程序,其中包含了倒数计时器,我为此使用了一个计时表......

现在有一种情况是,每 10 秒后,我使用 handler.postdelyed() 的计时器应该更改 1 秒,但在这种情况下,计时器在 10 秒后开始运行,但计时器的计时器从 10 秒开始,而不是 1 秒。有什么方法可以使计时器延迟计时。

这是我的代码:

chronometer_life_gained.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(final Chronometer chronometer) {
long delay = (206 / Long.parseLong(joint_per_day))*1000;
System.out.println("delayer " + delay);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));

year_num_life_gained.setVisibility(View.GONE);
year_text_life_gained.setVisibility(View.GONE);
month_num_life_gained.setVisibility(View.GONE);
month_text_life_gained.setVisibility(View.GONE);
day_num_life_gained.setVisibility(View.GONE);
day_text_life_gained.setVisibility(View.GONE);
hour_num_life_gained.setVisibility(View.GONE);
hour_text_life_gained.setVisibility(View.GONE);
min_num_life_gained.setVisibility(View.GONE);
min_text_life_gained.setVisibility(View.GONE);
sec_num_life_gained.setVisibility(View.VISIBLE);
sec_text_life_gained.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
sec_num_life_gained.setLayoutParams(params);

sec_num_life_gained.setText(Long.toString(sec));


}
//
}
}, delay);

// }

}
});

提前致谢

最佳答案

你可以用CountDownTimer来实现它,虽然它的名字是count down timer,但是我们可以让它工作起来。

// Counter variable to show seconds increment
int counter = 4000;

// If you want to count until 60 seconds, put VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP = 60000 (in milliseconds)
elapsedTimer = new CountDownTimer(VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP, 5000) {
@Override
public void onTick(long l) {
// Logically, we need
long elapsedTime = SystemClock.elapsedRealtime() - startTime - counter;
long dy = TimeUnit.MILLISECONDS.toDays(elapsedTime);
final long yr = dy / 365;
dy %= 365;

final long mn = dy / 30;
dy %= 30;

final long hr = TimeUnit.MILLISECONDS.toHours(elapsedTime)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(elapsedTime));
final long min = TimeUnit.MILLISECONDS.toMinutes(elapsedTime)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(elapsedTime));
final long sec = TimeUnit.MILLISECONDS.toSeconds(elapsedTime)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime));
// add 4000 as 4 seconds to counter value
counter = counter + 4000;
txView.setText(counter + " second");
}
}.start();

关于android - 在运行计时器时延迟时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59532690/

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