首页 文章

在Android中暂停和恢复倒计时器和进度条?

提问于
浏览
0

我正在制作一个简单的应用程序,它使用倒计时器,循环进度条和3个按钮启动,暂停和恢复 . 我想要做的是当特定活动开始时我按暂停它存储定时器的时间暂停并从那一点开始恢复 . 但问题是倒计时器没有停止,所以如果我在7秒暂停,进度条在7秒停止但是当我按下恢复它从任何倒数计时器值开始那一刻 . 这是我试图实现的代码 .

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();
}
    });

这是倒数计时器的代码 .

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();
    }

我是编程的新手,所以任何帮助或建议都表示赞赏 . 谢谢 .

1 回答

  • 1

    请参考:

    How to clear CountDownTimer from onTick method?

    我不确定你是否可以禁用计时器从内部onTick()方法倒计时 .

    为CountDownTimer创建一个gloabl引用,并且只从该引用上的clickListeners调用start()或cancel() .

相关问题