N
N
Nikita2017-10-20 02:05:21
Java
Nikita, 2017-10-20 02:05:21

How to interrupt CountDownTimer via onPause and onResume?

Pressing the button starts the timer. As soon as the application disappeared, that is, the onPause () method worked, the timer turned off and recorded the number of seconds elapsed in a variable. Now in onResume you need to call the timer again and pass it the value of the variable (time) on which the timer stopped
. Doesn't work. Tell me what might be wrong. Perhaps there is another way to implement, ready to accept links to tutorial =)

//
Переменные для хранения секунд и флаг для кнопки
long s1;
Boolean flag = true;
CountDownTimer count;
//
метод onClick()
//
..
public void timer(){
        if (testFlag){
            testFlag = false;
            Log.d(LOG_TAG, "Зашли в IF");
            //Действие
         count = new CountDownTimer(30000, 1000) {
                public void onTick(long millisUntilFinished) {
                    s1 = millisUntilFinished;
                    // mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                }
                public void onFinish() {
                    Log.d(LOG_TAG, "Зашли в метод финиш");
                    window();
                }
            }.start();
        }else{
            Log.d(LOG_TAG, "Else");
        }
    }
    @Override
    protected void onPause() {
        Log.d(LOG_TAG, "onPause");
        super.onPause();
        count.cancel();
    }
    @Override
    protected void onResume(){
        Log.d(LOG_TAG, "onResume");
        super.onResume();
        count = new CountDownTimer(s1, 1000) {
            public void onTick(long millisUntilFinished) {
                //s1 = millisUntilFinished;
                // mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
            }
            public void onFinish() {
                Log.d(LOG_TAG, "Зашли в метод финиш");
                window();
            }
        }.start();
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question