Timer和Handler实现轮询

 private static final int time = 1000;
 private TimerTask mTimerTask;



    /*
    *   delay:延时时间(毫秒)
        pre:轮询间隔(毫秒)
       mhandler:用来执行轮询的Handler
    * */

    public Timer startTimer(long delay, long pre, final Handler mHandeler1) {
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                mHandeler.sendEmptyMessage(time);
            }
        };
        Timer timer = new Timer();
        timer.schedule(mTimerTask, 300, 1000);
        return timer;
    }



    private Handler mHandeler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            {
                switch (msg.what) {

                   //要轮询的功能
                    case time:
                }
            }
        }
    };

 

你可能感兴趣的:(Timer和Handler实现轮询)