CountDownTimer не останавливается - PullRequest
0 голосов
/ 16 июня 2020

Пытаюсь сделать спортивный таймер. У меня есть два таймера: один для работы, один для отдыха. Каждый из них запускает предыдущий, пока не закончится количество подходов. Когда я пытаюсь установить для них паузу, он срабатывает только один раз за все время действия таймеров. То есть я могу остановить любое из них в любой момент, но только 1 раз. Более длительная пауза не работает. Что не так?

    timerSportPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isWorkTimer == true) {
                countDownTimerSport.cancel();
            }

            if (isRestTimer == true) {
                countDownTimerSportRest.cancel();
            }

            timerSportResume.setVisibility(View.VISIBLE);
            timerSportPause.setVisibility(View.INVISIBLE);
        }
    });

    timerSportResume.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (isWorkTimer == true) {
                startTimerSport();
                countDownTimerSport.start();
            }

            if (isRestTimer == true) {
                startTimerSportRest();
                countDownTimerSportRest.start();
            }

            timerSportPause.setVisibility(View.VISIBLE);
            timerSportResume.setVisibility(View.INVISIBLE);
        }
    });

Вот сами таймеры

    //timer sport work
private void startTimerSport () {

    countDownTimerSport = new CountDownTimer(timeSport, 1) {
        @Override
        public void onTick(long millisUntilFinished) {
            timeSport = (int) millisUntilFinished;
            progressSport = (int) (millisUntilFinished - 999);
            progressBarSport.setProgress(progressSport);
            int minutes = (timeSport / (1000 * 60) % 60);
            int seconds = (timeSport / 1000) % 60;
            String output = String.format("%02d : %02d", minutes, seconds);
            textTimerSport.setText(output);
        }

        @Override
        public void onFinish() {
            timerRepeat();
        }
    }.start();
}

//timer sport rest
private void startTimerSportRest () {

    countDownTimerSportRest = new CountDownTimer (timeSportRest, 1) {
        @Override
        public void onTick(long millisUntilFinished) {
            timeSportRest = (int) millisUntilFinished;
            progressSport = (int) (millisUntilFinished - 999);
            progressBarSport.setProgress(progressSport);
            int minutes = (timeSportRest / (1000 * 60) % 60);
            int seconds = (timeSportRest / 1000) % 60;
            String output = String.format("%02d : %02d", minutes, seconds);
            textTimerSport.setText(output);
        }

        @Override
        public void onFinish() {
            timerRepeat();
        }
    }.start();
}

private void timerRepeat() {
    countTimerSport--;

    if (countTimerSport % 2 == 0) {
        timeSport = timeConstSport;
        progressBarSport.setMax(timeConstSport);
        progressBarSport.setProgress(timeConstSport);
        isWorkTimer = true;
        isRestTimer = false;
        startTimerSport();
    } else {
        timeSportRest = timeConstSportRest;
        progressBarSport.setMax(timeConstSportRest);
        progressBarSport.setProgress(timeConstSportRest);
        isWorkTimer = false;
        isRestTimer = true;
        startTimerSportRest();
    }
}
...