Android Handler杂记(结合别人的blog)

handler泄露问题:

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1123/2047.html

http://blog.csdn.net/FeeLang/article/details/39059705

staticinner class里面都会有一个this$0的字段保存它的父对象。在Java中,非静态(匿名)内部类会默认隐性引用外部类对象。而静态内部类不会引用外部类对象

new Runnable是一个匿名内部类,所以这个就是一个隐式引用造成的memory leak--因为Runnable不是static类型,所以会有一个包含Activity实例的implicit reference --- Activity.this所以要使用static静态字段重写继承原来的类,

 

假设在runnable执行之前,View被移除了,但是成员变量view还在继续引用它,仍然会导致memory leak。显式引用。

用弱引用得到mTextView

或者在activity结束时在onDestroy中清理掉所有Messagesmhandler.removeCallbacksAndMessagesnull

 

 

 

 

Handler与Looper,MessageQueue之间的关系:

http://my.oschina.net/u/156598/blog/87454

 

至于为何在线程接收消息的时候需要Looper.prepare(),loop()前写执行代码,在quit()时停止消息循环

http://www.2cto.com/kf/201403/287585.html

http://www.cnblogs.com/tt_mc/archive/2012/01/30/2331876.html

Looper.prepare();单例一个looper实例,若无则会创造一个消息队列mQueue = new MessageQueue(),并绑定到现有线程上,mThread = Thread.currentThread();

 

蓝色部分是在线程中我们的网络访问方式还是不能用post来解决,需要先得到该结果后用post将消息传回给UI线程处理。

这个过程对应的是蓝色部分。

下图是Handler类的部分代码,见http://www.cnblogs.com/tt_mc/archive/2012/01/30/2331876.html

 

posted @ 2016-03-13 20:31  KiddingJohn  阅读(109)  评论(0编辑  收藏  举报