Android - Timer , Task , Handler

摘要:Android - Timer , Task , Handler

我現在才知道Handler有這種方法,以前都以為一定要用Message,看了別本書,才突然學會某種東西。

 


   
    private Handler mHandler = new Handler();
    private Runnable runable = new Runnable() {
        public void run() {
            doing();
        }
    };
  
    private synchronized void doing(){
        
    }
    private Task task = new Task();
    private Timer timer;
    class Task extends TimerTask {
        @Override
        public void run() {
            execute();
        } 
        
        public synchronized void execute() {
            mHandler.removeCallbacks(runable);
            mHandler.post(runable);
        }
    }

使用


       timer = new Timer();
        task = new Task();
        timer.schedule(task, 1000,500);