码迷,mamicode.com
首页 > 其他好文 > 详细

115、定时器(TimerTask+Timer+Handler)

时间:2016-11-30 13:48:53      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:sage   default   str   model   and   eth   bsp   string   base64编码   

public class TimerUtils {
    public static Activity act;
    public static List<MaiDianModels> listMaiAll = new ArrayList<MaiDianModels>();

    public TimerUtils(Activity act) {
        this.act = act;
    }

    public static Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 3199:
                    if (listMaiAll.size() > 0) {
                        // Gson gson = new Gson();
                        String str = new Gson().toJson(listMaiAll);

                        //    String类型 进行Base64编码
                        String encoded = Base64.encode(str);
                        Log.i("maidian", "encoded[编码]......" + encoded);

                        //    String类型 进行Base64解码
                        String decoded = Base64.decode(encoded);
                        Log.i("maidian", "decoded[解码]......" + decoded);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    public static Timer timer = null;
    public static MyTask myTask = null;

    static class MyTask extends TimerTask {   // 
        @Override
        public void run() {
            //do something
            Message msg = Message.obtain();
            msg.what = 3199;
            myHandler.sendMessage(msg);
        }
    }
    ;
    public static void start() {
        stop();
        timer = new Timer();
        myTask = new MyTask();
        timer.schedule(myTask, 2000, 2000); // 2s后执行task,经过2s再次执行  20000
    }

    public static void stop() {
        if (myTask != null) {
            myTask.cancel();
        }
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;  // (可选)
        }
    }
}
 new TimerUtils(TwoActivity.this).start();

 

115、定时器(TimerTask+Timer+Handler)

标签:sage   default   str   model   and   eth   bsp   string   base64编码   

原文地址:http://www.cnblogs.com/androidsj/p/6117127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!