美文网首页
Handler分析

Handler分析

作者: Super_me14 | 来源:发表于2017-08-17 15:35 被阅读9次

handler原理叙述

通过Looper.prepare保存当前线程的Looper对象,threadlocal能对同一线程保存一个Looper对象,主线程在androidthread生成保存Looper对象,所以无需调用prepare。Looper对象中持有MessageQueue对象,在Handler构造函数中,获取当前线程的looper和messageQueue对象的引用,handler通过调用post和send发送消息实质上就是把消息加入到消息队列中去,looper调用loop方法循环消息队列,把消息发送到handler的handmessage中去处理消息。

处理handler引发的内存泄露

  • hander采用静态内部类
  • 用软引用来持有外部对象
  • 调用removecallback方法规避内存泄露。

相关文章

网友评论

      本文标题:Handler分析

      本文链接:https://www.haomeiwen.com/subject/ksfnrxtx.html