Мой таймер принимает пользовательский ввод, чтобы установить время и запускает другой таймер обратного отсчета на fini sh (), я также обновляю textView, который указывает оставшееся время. Мне нужно, чтобы тикание продолжалось и запускался другой таймер, пока приложение закрыто, при повторном запуске мне нужно обновить текст. Я подумываю использовать для этого службу, но если у вас есть варианты получше, я открыт для всех. Спасибо за вашу помощь.
//This is the first timer
public void startWork() {
buttonReset.setVisibility(View.INVISIBLE);
resetRest();
updateRestText();
workEndTime = System.currentTimeMillis() + workTimeLeftInMillis;
workCountDownTimer = new CountDownTimer(workTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
workTimeLeftInMillis = millisUntilFinished;
updateWorkText();
}
@Override
public void onFinish() {
workTimeRunning = false;
startRest();
}
}.start();
workTimeRunning = true;
updateWorkText();
}
//This is the second timer
public void startRest() {
buttonReset.setVisibility(View.INVISIBLE);
resetWork();
updateWorkText();
restEndTime = System.currentTimeMillis() + restTimeLeftInMillis;
restCountDownTimer = new CountDownTimer(restTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
restTimeLeftInMillis = millisUntilFinished;
updateRestText();
}
@Override
public void onFinish() {
restTimeRunning = false;
startWork();
cycleCount ++;
}
}.start();
restTimeRunning = true;
updateRestText();
}
Функции сброса устанавливают время CountDownTimer обратно на ввод пользователя. Функции updateText делают это:
public void updateWorkText() {
int hours = (int) (workTimeLeftInMillis/1000) / 3600;
int minutes = (int) ((workTimeLeftInMillis / 1000) % 3600) / 60;
int seconds = (int) (workTimeLeftInMillis / 1000 % 60);
String workTimeLeftFormatted;
if (hours>0) {
workTimeLeftFormatted = String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, seconds);
} else {workTimeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
}
workTextViewCountDown.setText(workTimeLeftFormatted);
}