1. Handler的作用 用于在非UI线程更新UI 可以主线程发送延迟消息 2. 为什么android 设计只能UI线程更新UI 解决多线程并发问题 提高界面更新的性能问题 架构设计的简单3. Handler的相关异常 android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 产生原因:在非UI线程更新UI 解决方案:Handler向主线程发送消息,让主线程更新UI,在Handler的handMessage方法中处理更新UI界面信息Can't create handler inside thread that has not called Looper.prepare() 4. Handler,Looper,MessageQueue之间的关系 一、Handler 封装了消息的发送(主要包括消息发送给谁)Looper 1.内部包含一个消息队列也就是MessageQueue,所有的Handler发送的消息都走向这个消息队列2. Looper.Looper方法就是一个死循环,不断的从MessageQueue取消息,如有消息就处理消息,没有消息就阻塞。 二、MessageQueue,就是一个消息队列,可以添加消息,并处理消息三、Handler也很简单,内部会跟Looper进行关联,也就是说Handler的内部可以找到Looper,找到了Looper也就找到了MessageQueue,在Handler中发送消息,其实就是向MessageQueue队列中发送消息。 总结:Handler负责发送消息,Looper负责接受Handler发送的消息,并直接把消息回传给Handler自己,MessageQueue就是一个存储消息的容器。 举例: 3. 根线程相关的Handler(HandlerThread)? 4. 主线程往子线程发送消息(考验你是否真的理解Handler机制)? 5. 能直接在子线程中更新UI吗? android 是如何检测非UI线程去更新UI的? ViewRootImp是什么? ViewRootImp是在哪里创建的? 6.参考: http://www.cnblogs.com/codingmyworld/archive/2011/09/14/2174255.html
|
|
来自: Joselyn_cui > 《android》