我现在是通过两个按钮来控制;一个是"开始截图",一个是"停止截图"
现在我用到的是TimerTask,每隔3秒截一张图,
但是当我点"停止截图"按钮后,Timer就直接Cancel掉了
然后我再想开始截图,就会报Timer already cancelled
也就是已经取消了Timer大家有什么好的办法,帮我解决一下,谢谢...
现在我用到的是TimerTask,每隔3秒截一张图,
但是当我点"停止截图"按钮后,Timer就直接Cancel掉了
然后我再想开始截图,就会报Timer already cancelled
也就是已经取消了Timer大家有什么好的办法,帮我解决一下,谢谢...
{
if(flag)
{
//do things
}
}当界面点击暂停,flag设为false
当界面点击开始, flag设为true,
另外, timer这东西不好用,以前做一个东西, timer到一定上限就不跑了, 后来我自己写了一个线程搞定的.长时间运行, 建议别用这玩意, 自己写个线程,也是一个道理
* This class implements the same API as the Java 1.3 java.util.TimerTask.
* Note that a TimerTask can only be scheduled on one Timer at a time, but
* that this implementation does not enforce that constraint.
**/
public abstract class TimerTask implements Runnable {
boolean cancelled = false; // Has it been cancelled?
long nextTime = -1; // When is it next scheduled?
long period; // What is the execution interval
boolean fixedRate; // Fixed-rate execution? protected TimerTask() {} /**
* Cancel the execution of the task. Return true if it was actually
* running, or false if it was already cancelled or never scheduled.
**/
public boolean cancel() {
if (cancelled) return false; // Already cancelled;
cancelled = true; // Cancel it
if (nextTime == -1) return false; // Never scheduled;
return true;
} /**
* When it the timer scheduled to execute? The run() method can use this
* to see whether it was invoked when it was supposed to be
**/
public long scheduledExecutionTime() { return nextTime; } /**
* Subclasses must override this to provide that code that is to be run.
* The Timer class will invoke this from its internal thread.
**/
public abstract void run(); // This method is used by Timer to tell the Task how it is scheduled.
void schedule(long nextTime, long period, boolean fixedRate) {
this.nextTime = nextTime;
this.period = period;
this.fixedRate = fixedRate;
} // This will be called by Timer after Timer calls the run method.
boolean reschedule() {
if (period == 0 || cancelled) return false; // Don't run it again
if (fixedRate) nextTime += period;
else nextTime = System.currentTimeMillis() + period;
return true;
}
}
@Override
public void run() {
resultArea.setSelectionStart(resultArea.getText().length());
}
};
timer = new Timer();
timer.schedule(task, 0, 1000);
我也用这个呢标志位的方法可行
timetask任务取消了,确实就无法恢复了