更改计时器计划scheduleAtFixedRate

时间:2013-06-06 17:56:51

标签: android timer

我有TextView经常更新工作正常,但问题是我想更改不同条件的计划时间,但我无法更改计划时间。它最初会继续设定时间。

private Long mPeriod = Long.parseLong("8000");

public void startTimer() {
    Logger.i("startTimer", "startTimer");

    stopTimer();
    mTimer = new Timer();
    mTimerTask = new TimerTask() {
        @Override
        public void run() {
            // postInvalidate();
            Logger.i("timer", "timer");
            Thread th = new Thread(new Runnable() {

                @Override
                public void run() {
                    Logger.i("thread", "thread");

                    Long ll = System.currentTimeMillis();

                    mRelativeTime = DateUtils.getRelativeTimeSpanString(
                            Long.parseLong(mText + ""), ll, 0);

                    Long diff = ll - Long.parseLong(mText + "");
                    // long diffSeconds = diff / 1000 % 60;
                    long diffMinutes = diff / (60 * 1000) % 60;
                    long diffHours = diff / (60 * 60 * 1000) % 60;

                    Logger.d("diffMinutes", diffMinutes + "");
                    Logger.d("diffHours", diffHours + "");
                    if (diffMinutes == 0 && diffHours == 0) {
                        Logger.i("5000", "5000");
                        mPeriod = Long.parseLong("5000");
                    } else if (diffMinutes < 60 && diffMinutes != 0
                            && diffHours == 0) {
                        Logger.i("30000", "30000");
                        mPeriod = Long.parseLong("30000");
                    } else if (diffHours > 0) {
                        Logger.i("600000", "600000");
                        mPeriod = Long.parseLong("600000");
                    }
                    **// This line is not working...
                    mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);**
                    // Change text
                    handler.sendEmptyMessage(0);

                }
            });
            th.run();
        }
    };
    mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);
}

public void stopTimer() {

    if (mTimer != null) {
        mTimer.cancel();
        mTimer = null;
    }

}

2 个答案:

答案 0 :(得分:3)

您必须创建自己的CustomTimerTask类,该类从TimerTask扩展。在此之后,您可以使用mCustomTimerTask(CustomTimerTask的对象)重新安排您的时间。 Here是可能重复的答案。

答案 1 :(得分:2)

我已经解决了我的问题。只需检查上一个延迟,下面是解决方案。

private Long mPeriod = Long.parseLong("8000");

public void startTimer() {

Logger.i("startTimer", "startTimer");

stopTimer();
mTimer = new Timer();
mTimerTask = new TimerTask() {
    @Override
    public void run() {
        // postInvalidate();
        Logger.i("timer", "timer");
        Thread th = new Thread(new Runnable() {

            @Override
            public void run() {
                Logger.i("thread", "thread");

                Long ll = System.currentTimeMillis();

                mRelativeTime = DateUtils.getRelativeTimeSpanString(
                        Long.parseLong(mText + ""), ll, 0);

                Long diff = ll - Long.parseLong(mText + "");
                // long diffSeconds = diff / 1000 % 60;
                long diffMinutes = diff / (60 * 1000) % 60;
                long diffHours = diff / (60 * 60 * 1000) % 60;

                Logger.d("diffMinutes", diffMinutes + "");
                Logger.d("diffHours", diffHours + "");
                if (diffMinutes == 0 && diffHours == 0 && mTimerTask != 5000) {
                    Logger.i("5000", "5000");
                    mPeriod = Long.parseLong("5000");
                    startTimer();
                } else if (diffMinutes < 60 && diffMinutes != 0
                        && diffHours == 0 && mTimerTask != 30000) {
                    Logger.i("30000", "30000");
                    mPeriod = Long.parseLong("30000");
                    startTimer();
                } else if (diffHours > 0 && mTimerTask != 600000) {
                    Logger.i("600000", "600000");
                    mPeriod = Long.parseLong("600000");
                    startTimer();
                }

                // Change text
                handler.sendEmptyMessage(0);

            }
        });
        th.run();
    }
};
mTimer.scheduleAtFixedRate(mTimerTask, 0, mPeriod);
}

public void stopTimer() {
if (mTimer != null) {
    mTimer.cancel();
    mTimer = null;
}

}