Android Handler 原理分析及实例代码
android handler 原理分析
handler一个让无数android开发者头疼的东西,希望我今天这边文章能为您彻底根治这个问题
今天就为大家详细剖析下handler的原理
handler使用的原因
1.多线程更新ui会导致ui界面错乱
2.如果加锁会导致性能下降
3.只在主线程去更新ui,轮询处理
handler使用简介
其实关键方法就2个一个sendmessage,用来接收消息
另一个是handlemessage,用来处理接收到的消息
下面是我参考疯狂android讲义,写的一个子线程和主线程之间相互通信的demo
对原demo做了一定修改
public class mainactivity extends appcompatactivity { public final static string upper_num="upper_num"; private edittext edittext; public jisuanthread jisuan; public handler mainhandler; private textview textview; class jisuanthread extends thread{ public handler mhandler; @override public void run() { looper.prepare(); final arraylist<integer> al=new arraylist<>(); mhandler=new handler(){ @override public void handlemessage(message msg) { if(msg.what==0x123){ bundle bundle=msg.getdata(); int up=bundle.getint(upper_num); outer: for(int i=3;i<=up;i++){ for(int j=2;j<=math.sqrt(i);j++){ if(i%j==0){ continue outer; } } al.add(i); } message message=new message(); message.what=0x124; bundle bundle1=new bundle(); bundle1.putintegerarraylist("result",al); message.setdata(bundle1); mainhandler.sendmessage(message); } } }; looper.loop(); } } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext= (edittext) findviewbyid(r.id.et_num); textview= (textview) findviewbyid(r.id.tv_show); jisuan=new jisuanthread(); jisuan.start(); mainhandler=new handler(){ @override public void handlemessage(message msg) { if(msg.what==0x124){ bundle bundle=new bundle(); bundle=msg.getdata(); arraylist<integer> al=bundle.getintegerarraylist("result"); textview.settext(al.tostring()); } } }; findviewbyid(r.id.bt_jisuan).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { message message=new message(); message.what=0x123; bundle bundle=new bundle(); bundle.putint(upper_num, integer.parseint(edittext.gettext().tostring())); message.setdata(bundle); jisuan.mhandler.sendmessage(message); } }); } }
hanler和looper,messagequeue原理分析
1.handler发送消息处理消息(一般都是将消息发送给自己),因为hanler在不同线程是可使用的
2.looper管理messagequeue
looper.loop死循环,不断从messagequeue取消息,如果有消息就处理消息,没有消息就阻塞
public static void loop() { final looper me = mylooper(); if (me == null) { throw new runtimeexception("no looper; looper.prepare() wasn't called on this thread."); } final messagequeue queue = me.mqueue; // make sure the identity of this thread is that of the local process, // and keep track of what that identity token actually is. binder.clearcallingidentity(); final long ident = binder.clearcallingidentity(); for (;;) { message msg = queue.next(); // might block if (msg == null) { // no message indicates that the message queue is quitting. return; } // this must be in a local variable, in case a ui event sets the logger printer logging = me.mlogging; if (logging != null) { logging.println(">>>>> dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchmessage(msg); if (logging != null) { logging.println("<<<<< finished to " + msg.target + " " + msg.callback); } // make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newident = binder.clearcallingidentity(); if (ident != newident) { log.wtf(tag, "thread identity changed from 0x" + long.tohexstring(ident) + " to 0x" + long.tohexstring(newident) + " while dispatching to " + msg.target.getclass().getname() + " " + msg.callback + " what=" + msg.what); } msg.recycleunchecked(); } }
这个是looper.loop的源码,实质就是一个死循环,不断读取自己的messqueue的消息
3.messqueue一个消息队列,handler发送的消息会添加到与自己内联的looper的messqueue中,受looper管理
private looper(boolean quitallowed) { mqueue = new messagequeue(quitallowed); mthread = thread.currentthread(); }
这个是looper构造器,其中做了2个工作,
1.生成与自己关联的message
2.绑定到当前线程
主线程在初始化的时候已经生成looper,
其他线程如果想使用handler需要通过looper.prepare()生成一个自己线程绑定的looper
这就是looper.prepare()源码,其实质也是使用构造器生成一个looper
private static void prepare(boolean quitallowed) { if (sthreadlocal.get() != null) { throw new runtimeexception("only one looper may be created per thread"); } sthreadlocal.set(new looper(quitallowed)); }
4.handler发送消息会将消息保存在自己相关联的looper的messagequeue中,那它是如何找到这个messagequeue的呢
public handler(callback callback, boolean async) { if (find_potential_leaks) { final class<? extends handler> klass = getclass(); if ((klass.isanonymousclass() || klass.ismemberclass() || klass.islocalclass()) && (klass.getmodifiers() & modifier.static) == 0) { log.w(tag, "the following handler class should be static or leaks might occur: " + klass.getcanonicalname()); } } mlooper = looper.mylooper(); if (mlooper == null) { throw new runtimeexception( "can't create handler inside thread that has not called looper.prepare()"); } mqueue = mlooper.mqueue; mcallback = callback; masynchronous = async; }
这个是handler的构造方法,它会找到一个自己关联的一个looper
public static looper mylooper() { return sthreadlocal.get(); }
没错,他们之间也是通过线程关联的,得到looper之后自然就可以获得它的messagequeue了
5.我们再看下handler如发送消息,又是如何在发送完消息后,回调handlermessage的
private boolean enqueuemessage(messagequeue queue, message msg, long uptimemillis) { msg.target = this; if (masynchronous) { msg.setasynchronous(true); } return queue.enqueuemessage(msg, uptimemillis); }
这个就是handler发送消息的最终源码,可见就是将一个message添加到messagequeue中,那为什么发送完消息又能及时回调handlemessage方法呢
大家请看上边那个loop方法,其中的for循环里面有一句话msg.target.dispatchmessage(msg);
public void dispatchmessage(message msg) { if (msg.callback != null) { handlecallback(msg); } else { if (mcallback != null) { if (mcallback.handlemessage(msg)) { return; } } handlemessage(msg); } }
这就是这句话,看到了吧里面会调用hanlermessage,一切都联系起来了吧
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!