//1 post postDelay Handler.post / postDelay (new Runnable() { @Override voidrun(){ //todo } })
//2 sendMessage
创建handler 实现handlerMessage接口
handler.sendMessage(msg)
Activity finish时handler未执行完会引发Activity内存泄漏?
优化
通过静态内部类+弱引用方式实现handler
等等
HandlerThread
HandlerThread继承Thread.
run方法内进行Looper的初始化相关操作
Looper
Looper.prepare
从TheadLocal中取得Looper对象,执行多次会抛出异常
1 2 3 4 5 6 7 8 9 10 11 12 13
staticfinal ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>(); //... publicstaticvoidprepare(){ prepare(true); }
privatestaticvoidprepare(boolean quitAllowed){ if (sThreadLocal.get() != null) { thrownew RuntimeException("Only on lopper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }