单击停止按钮后 TimerTask 没有停止

TimerTask not stopping after I click the stopbutton

所以我点击开始按钮,每一秒我都在计算时间来更改标签的文本:

new CountDownTimer(30000, 500) {

    public void onTick(long millisUntilFinished) {

        if (zaciatokgotten) {

            T2 = new Timer();
            tt2 = new TimerTask() {

                public void run() {
                    if(stopclicked == true) {
                        this.cancel();
                    }
                    handler.post(new Runnable() {

                        public void run() {
                            hasStartedtt2 = true;
                            calendar = Calendar.getInstance();
                            simpledate = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                            do_casu = simpledate.format(calendar.getTime());

                            zaciatok_hour = zaciatok.substring(11, 13);
                            zaciatok_minute = zaciatok.substring(14, 16);
                            koniec_hour = do_casu.substring(11, 13);
                            koniec_minute = do_casu.substring(14, 16);

                            zaciatok_sekundy = zaciatok.substring(17, 19);
                            koniec_sekundy = do_casu.substring(17, 19);


                            final_hour = ((Integer.parseInt(koniec_hour) - Integer.parseInt(zaciatok_hour)) );
                            final_minute = Integer.parseInt(koniec_minute) - Integer.parseInt(zaciatok_minute);
                            final_seconds = Integer.parseInt(koniec_sekundy) - Integer.parseInt(zaciatok_sekundy) - 1;


                            if (final_seconds < 0) {
                                final_seconds = final_seconds + 60;
                                final_minute = final_minute - 1;
                            }

                            if (final_minute < 0) {
                                final_hour = final_hour - 1;
                                final_minute = final_minute + 60;
                            }
                            if (final_hour < 0) {
                                final_hour = 0;
                            }
                            if (final_minute < 0) {
                                final_minute = 0;
                            }

                            if (final_hour == 0) {
                                if (final_minute == 0) {
                                    txtProgress.setText(final_seconds + " s");
                                } else {
                                    txtProgress.setText(final_minute + " m " + final_seconds + " s");
                                }
                            } else {
                                txtProgress.setText(final_hour + " h " + final_minute + " m " + final_seconds + " s");
                            }
                        }
                    });
                }
            };

            T2.schedule(tt2, 1000, 1000);

            if(once ==0) {
                new Thread(new Runnable() {

                    @Override
                    public void run() {

                        while (pStatus <= 100) {

                            handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    progressBar.setProgress(pStatus);
                                }
                            });
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        pStatus++;
                        if (pStatus == 100) {
                            pStatus = 0;
                        }
                    }
                }
            }).start();

            once++;
            this.cancel();
        }
    }}

    public void onFinish() {
    }
}.start();

点击停止按钮后progressBar停止旋转,这是正确的,但时间一直在增加,我做错了什么?请注意,这个问题不存在于我用来测试它的手机上,但存在于平板设备上,也许它们处理线程的方式不同?

这里是停止定时器的代码:

stopclicked = true
if(hasStartedtt2 == true) {
    tt2.cancel();
    //  handler.removeCallbacksAndMessages(null);
}
if (T2 != null) {
    T2.cancel();
    T2.purge();
}
pStatus = 101;

EDIT1:我刚刚注意到它已停止,但在我单击“停止”后仅 20 - 25 秒,是什么导致了延迟?

时间不会停止(或停止 "only 20 - 25 seconds after you clicked Stop")的原因是您创建的 T2 = new Timer()tt2 = new TimerTask() 的新实例每个 CountDownTimeronTick()TimerTimerTask 的旧实例继续驻留在 Java 堆上,直到被垃圾收集为止。您可能会注意到时间变化是颠簸的。

而且,你好像overcomplicate the task implementation。您可能既不需要 Timer 也不需要 TimerTask。下面的示例停止了时间,只使用 CountDownTimer 而没有 TimerTimerTask。为了简洁起见,我稍微简化了代码。

private CountDownTimer timer;
private TextView txtProgress;
private Button stopButton;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtProgress = findViewById(R.id.tv);

    timer = new CountDownTimer(30000, 500) {

        public void onTick(long millisUntilFinished) {
                    handler.post(() -> {
                        txtProgress.setText("any text");
                    });
        }

        public void onFinish() { }

    }.start();

    findViewById(R.id.stopButton).setOnClickListener(v -> timer.cancel());
}