简体   繁体   中英

Pausing and Resuming a count down timer and progress bar in android?

I am making a simple app which uses a count down timer,Circular progress bar and 3 buttons start, Pause and Resume.What I am trying to do is when a particular activity starts and I press pause it stores the time at which the timer was paused and resume from that point forward.But the problem is the count down timmer doesn't stop so if I pause at say 7 sec the progress bar stops at 7sec but when I press resume it starts from whatever the count down timer value is at that moment.This is the code I am trying to implement.

  btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //When user request to pause the CountDownTimer
                isPaused = true;

                //Enable the resume and cancel button
                btnResume.setEnabled(true);
                //Disable the start and pause button
                btnStart.setEnabled(false);
                btnPause.setEnabled(false);
            }
        });



btnResume.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Disable the start and resume button

        btnStart.setEnabled(false);
        btnResume.setEnabled(false);
        //Enable the pause and cancel button
        btnPause.setEnabled(true);

        //Specify the current state is not paused and canceled.
        isPaused = false;
        isCanceled = false;
        mCountDownTimer.start();
}
    });

And this is code for the count down timer.

 private void neck_rotations()
    {
        neckRotation = true;
        mCountDownTimer = new CountDownTimer(NECK_ROTATION_TIME, 1000)
        {
            @Override
            public void onTick(long millisUntilFinished)
            {

                if (isPaused || isCanceled)
                {
                    paused_at = (int) (millisUntilFinished / 1000);

                } else
                {
                    timeRemaining = (int) (millisUntilFinished / 1000);
                    donutProgress.setMaxValue(NECK_ROTATION_TIME / 1000);
                    donutProgress.setValueAnimated((int) timeRemaining);
                    counter.setText(String.valueOf(timeRemaining));

                }

            }

            @Override
            public void onFinish()
            {
                response = "Jumps";
                rest(response);

            }
        };
        mCountDownTimer.start();
    }

I am new to programming so any help or suggestion is appreciated.Thank you.

Please refer to this:

How to clear CountDownTimer from onTick method?

I'm not sure you can disable the timer from still counting down from the inner onTick() method.

Make a gloabl reference for the CountDownTimer and only call start() or cancel() from the clickListeners on that reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM