D
D
Denis2017-06-06 16:35:09
Java
Denis, 2017-06-06 16:35:09

Interval Timer Cycle - Why does not it work?

The idea is that the two timers run in sequence, and iterates n times depending on the value of numberRound. In fact, the sequence only fires once, and mTextRound shows the value numberRound+1.
Here is the code:

public void TimerWorkOut() {
        new CountDownTimer(secWork, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextWork.setText(" " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextWork.setText("Rest!");
                TimerRest();
            }
        }.start();
    }

    public void TimerRest() {
        new CountDownTimer(secRest, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextRest.setText(" " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextRest.setText("Just do it!");
            }
        }.start();
    }

    // Кнопки

    public void ClickStart(View view) {
        secWork = pickerSecWork.getValue() * 1000;
        secRest = pickerSecRest.getValue() * 1000;
        numberRound = pickerRound.getValue();
        while (n <= numberRound){
            TimerWorkOut();
            mTextRound.setText("0" + n);
            n = n + 1;
        }
    }

    // слушатели NumberPicker, и передача данных в TextWiew

    NumberPicker.OnValueChangeListener onValueChangedWork = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if (!mIsRunning) {
                mTextWork.setText(intToTime(newVal));
                mCurrentPeriod = newVal;
            }
        }
    };

    NumberPicker.OnValueChangeListener onValueChangedRest = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if (!mIsRunning) {
                mTextRest.setText(intToTime(newVal));
                mCurrentPeriod = newVal;
            }
        }
    };

    private String intToTime(int i) {
        return (new SimpleDateFormat("ss")).format(new Date(i * 1000));
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-06-07
@Manitu_PC

Here is a solution: the round counter has been moved to the onFinish TimerRest method. It looks like this:

public void TimerRest() {
        new CountDownTimer(secRest, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextRest.setText(" " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextRest.setText("Just do it!");
                if(n < numberRound) {
                    TimerWorkOut();
                    n++;
                }
                mTextRound.setText("№" + n);
            }
        }.start();
    }

    // Кнопки
    public void ClickStart(View view) {
        secWork = pickerSecWork.getValue() * 1000;
        secRest = pickerSecRest.getValue() * 1000;
        numberRound = pickerRound.getValue();
        TimerWorkOut();
        mTextRound.setText("№" + n);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question