更改计时器计划 scheduleAtFixedRate

Posted

技术标签:

【中文标题】更改计时器计划 scheduleAtFixedRate【英文标题】:Change Timer schedule scheduleAtFixedRate 【发布时间】:2013-06-06 17:56:50 【问题描述】:

我有 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;
    


【问题讨论】:

【参考方案1】:

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

【讨论】:

这是一个重复的问题。并由同一个人回答:P 这不是重复的问题。我想在 TimerTask 中重新安排我的计时器,它被视为递归,因为它在重新安排后启动 TimerTask。【参考方案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;



【讨论】:

以上是关于更改计时器计划 scheduleAtFixedRate的主要内容,如果未能解决你的问题,请参考以下文章

win10在休眠中执行定时任务

转:SharePoint2013TimerJob计时器发送邮件

计划计时器每月执行一次 - C#

使用 Juggernaut 同步客户端计时器时处理延迟

在 iOS 版 Firebase Analytics 中设置计划计时器

使用计时器在 JSF 托管 bean 中为计划任务生成线程