Я реализовал простой таймер, но столкнулся с проблемой задержки по времени, когда экран устройства продолжает оставаться активным, а таймер работает через 15–20 минут и показывает задержку в 1–2 минуты с точным временем.
long timerDuration = Long.MAX_VALUE; // Use a very large number to simulate indefinite counting
// Create and start the CountDownTimer
breakTimer = new CountDownTimer(timerDuration, 1000) { // Count down in intervals of 1 second
@Override
public void onTick(long millisUntilFinished) {
// Increment the break time
// Calculate hours, minutes, and seconds
int h = totalBreakSeconds / 3600;
int m = (totalBreakSeconds % 3600) / 60;
int s = totalBreakSeconds % 60;
// Update the UI
breakHours.setText(String.format("%02d", h));
breakMinutes.setText(String.format("%02d", m));
breakSeconds.setText(String.format("%02d", s));
totalBreakSeconds++;
}
@Override
public void onFinish() {
// You can implement logic here if you want to handle the end of the timer
// For this use case, it will not reach here because of Long.MAX_VALUE
}
}.start();
Я реализовал простой таймер, но столкнулся с проблемой задержки по времени, когда экран устройства продолжает оставаться активным, а таймер работает через 15–20 минут и показывает задержку в 1–2 минуты с точным временем. [code]long timerDuration = Long.MAX_VALUE; // Use a very large number to simulate indefinite counting
// Create and start the CountDownTimer breakTimer = new CountDownTimer(timerDuration, 1000) { // Count down in intervals of 1 second @Override public void onTick(long millisUntilFinished) { // Increment the break time
// Calculate hours, minutes, and seconds int h = totalBreakSeconds / 3600; int m = (totalBreakSeconds % 3600) / 60; int s = totalBreakSeconds % 60;
// Update the UI breakHours.setText(String.format("%02d", h)); breakMinutes.setText(String.format("%02d", m)); breakSeconds.setText(String.format("%02d", s));
totalBreakSeconds++; }
@Override public void onFinish() { // You can implement logic here if you want to handle the end of the timer // For this use case, it will not reach here because of Long.MAX_VALUE } }.start(); [/code] Кто-нибудь может помочь мне решить эту проблему.