我现在是通过两个按钮来控制;一个是"开始截图",一个是"停止截图"
现在我用到的是TimerTask,每隔3秒截一张图,
但是当我点"停止截图"按钮后,Timer就直接Cancel掉了
然后我再想开始截图,就会报Timer already cancelled
也就是已经取消了Timer大家有什么好的办法,帮我解决一下,谢谢...

解决方案 »

  1.   

    设置一个标志位,开始截图则设置flag为true当timer检查标志为true,则截图当停止截图点击的时候,设标志为false,
      

  2.   

    即:timer的run方法中run()
    {
      if(flag)
      {
        //do things
      }
    }当界面点击暂停,flag设为false
    当界面点击开始, flag设为true,
    另外, timer这东西不好用,以前做一个东西, timer到一定上限就不跑了, 后来我自己写了一个线程搞定的.长时间运行, 建议别用这玩意, 自己写个线程,也是一个道理
      

  3.   

    /**
     * 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;
        }
    }
      

  4.   

    TimerTask这个很容易出问题,不推荐用.
      

  5.   

    实现TextArea自动滚屏TimerTask task = new TimerTask() { // 实现结果自动滚屏
    @Override
    public void run() {
    resultArea.setSelectionStart(resultArea.getText().length());
    }
    };
    timer = new Timer();
    timer.schedule(task, 0, 1000);
      

  6.   

    那帮忙给LZ推荐一个呗
    我也用这个呢标志位的方法可行
    timetask任务取消了,确实就无法恢复了
      

  7.   

    可以不调用Timer的cancel,只使用TimerTask的cancel。开始按钮,创建一个TimerTask,停止按钮,取消一个TimerTask,Timer本身是不需要cancel的,直到退出程序的时候清理即可。
      

  8.   

    当你再想启动timer的时候,new一个新对象就可以了