单击“停止”按钮后,TimerTask没有停止

时间:2018-09-04 14:35:47

标签: java android timer android-handler

因此,我单击一个开始按钮,然后每隔一秒钟我就会通过计算时间来更改标签的文本:

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秒,是什么原因导致延迟?

1 个答案:

答案 0 :(得分:1)

时间不会停止的原因(或停止“仅在单击“停止”后20-25秒” ”停止)是您创建的T2 = new Timer()tt2 = new TimerTask()的新实例在每个CountDownTimer的{​​{1}}上。 onTick()Timer的旧实例一直驻留在 Java 堆上,直到获得垃圾回收为止。您可能会注意到时间变化坎being。

此外,您似乎overcomplicate the task implementation。您可能不需要TimerTaskTimer。以下示例停止了运行时间,仅使用TimerTask,而没有CountDownTimerTimer。为了简洁起见,我对代码进行了一些简化。

TimerTask