当前位置: 代码迷 >> J2SE >> TimerTask Timer如何样子重新schedule
  详细解决方案

TimerTask Timer如何样子重新schedule

热度:640   发布时间:2016-04-24 01:01:25.0
TimerTask Timer怎么样子重新schedule ?
TimerTask Timer怎么样子重新schedule ?

Timer 已经 schedule了,

mTimer.schedule(mWatchdogTask, 10 * 1000, mScreenTime);


public class WatchdogTask extends TimerTask
 {
@Override
public void run()
{
Log.d(TAG,"WatchdogTask run: " + mScreenOffTimeoutSetting );

}
}

现在问题是mScreenTime时间还没有到,我决定重新对mScreenTime赋值 再重新开始循环定时器,就不会了

我的做法是 先cancel
mTimer.cancel();
再重新new Timer 再mTimer.schedule,但是似乎不行!求教



------解决方案--------------------
Java code
        Timer t = new Timer();        TimerTask tt = new TimerTask()        {            private int time = 0;            @Override            public void run()            {                System.out.println(++time);                if (time >= 5)                {                    this.cancel();                }            }        };        t.schedule(tt, 0, 1000);                // sleep for 3 seconds        try        {            Thread.sleep(3000);        }        catch (InterruptedException e)        {            e.printStackTrace();        }                tt.cancel();        TimerTask tt1 = new TimerTask()        {            private int time = 0;            @Override            public void run()            {                System.out.println(++time);                if (time >= 5)                {                    this.cancel();                }            }        };        t.schedule(tt1, 0,1000);
  相关解决方案