N
N
Norton-nsk2020-07-06 12:17:08
Java
Norton-nsk, 2020-07-06 12:17:08

How to check before starting CountDownTimer that it is not running and if it is running, then reset it?

How to check before starting CountDownTimer that it is not running and if it is running, then reset it?

bt.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View view) {
                tv.setText("0");
                pb.setProgress(4);
                pb2.setProgress(4);
                pb3.setProgress(4);
                pb4.setProgress(4);
                //timer.cancel(); Собственно здесь и вопрос

                CountDownTimer timer = new CountDownTimer(4000, 1000) {
                    @Override
                    public void onTick(long l) {
                        tv.setText("" + l / 1000);
                        pb.setProgress((int) (l / 1000));
                    }

                    @Override
                    public void onFinish() {
                        tv.setText("0");
                        pb.setProgress(0);
                    }
                }.start();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Glebka_Greben, 2020-07-06
@Glebka_Greben

You need to check the value of the timer variable. If it is equal to 0, then the timer is not running, if not, then you need to reset the timer and start it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question