Android Handler 消息循环机制

it2022-05-05  246

前言

一问起Android应用程序的入口,很多人会说是Activity中的onCreate方法,也有人说是ActivityThread中的静态main方法。因为Java虚拟机在运行的时候会自动加载指定类的静态共有main方法,因此个人更倾向于第二种说法。

public final class ActivityThread
...... 
public static void main(String[] args)
...... 
Looper.prepareMainLooper(); 
ActivityThread thread = new ActivityThread(); 
thread.attach(false); 
if (sMainThreadHandler == null) { 
sMainThreadHandler = thread.getHandler(); 

...... 
Looper.loop(); 
throw new RuntimeException("Main thread loop unexpectedly exited"); 


我们注意到这段代码,先调用Looper的prepareMainLooper()方法,新建一个ActivityThread,然后再获取MainThreadHander,最后调用Looper.loop()方法,程序就一直在loop方法中循环。Looper,Handler之间有何关系?请看下文。

程序的消息循环

class test
public static void main (String[] args)
System.out.println("hello world!"); 


上述小程序就是一个任务,虚拟机启用一个线程执行完该任务后,程序就结束了。为了保证程序不立即退出,一般都会写一个循环

class test
public static void main (String[] args)
while (hasNextMessage()) { 
msg = getMessage() ;  
handleMessage(msg) ; 



系统不断从getMessage获取消息,再通过handleMessage来处理消息。这种基于消息的循环模型在许多的系统框架中都有实现。比如iOS系统中的RunLoop,再比如windows系统中的消息队列,windows系统会为每一个UI线程分配一个消息队列,发生输入事件后,windows将事件转换为一个"消息"发送给系统消息队列,操作系统有一个专门的线程从系统消息队列中取出消息,分发给各个UI线程的输入消息队列中。Android中的应用系统框架也不例外,也有一套自己的消息循环机制,这套机制是靠Looper、Handler、MessageQueue来共同完成的。

原理

p1.jpg 图片来自 zongpeiqing的博客。Looper负责消息循环(例子中的while语句),Handler负责发送和处理消息,MessageQueue则负责管理消息(消息的增加和移除)。不管是sendMessage还是sendMessageDelay或是View.post方法,或是上面列出的没列出的发送消息的方法,最终都会包装成一条消息Message(这条消息由Handler发出),然后调用MessageQuque的enqueueMessage方法把消息放到消息队列MessageQueue中。而Looper则会不停地查看MessageQueue中是否有新消息msg,有新消息就会调用新消息msg.target.handleMessage()去处理消息,否则会一直阻塞。msg.target实际上是Handler对象。因此,Handler发送的消息,最终也是由Handler来处理。

问题

Q1: Handler是怎么在不同线程之间切换的?

public final class Looper
...... 
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; 
........ 
for (;;) { 
Message msg = queue.next(); // might block 
if (msg == null) { 
// No message indicates that the message queue is quitting. 
return

...... 
msg.target.dispatchMessage(msg); 
...... 
msg.recycleUnchecked(); 



public class Handler
/* 
* Set this flag to true to detect anonymous, local or member classes 
* that extend this Handler class and that are not static. These kind 
* of classes can potentially create leaks. 
*/ 
private static final boolean FIND_POTENTIAL_LEAKS = false
private static final String TAG = "Handler"
 
/** 
* Callback interface you can use when instantiating a Handler to avoid 
* having to implement your own subclass of Handler. 
* 
* @param msg A {@link android.os.Message Message} object 
* @return True if no further handling is desired 
*/ 
public interface Callback
public boolean handleMessage(Message msg)

 
/** 
* Subclasses must implement this to receive messages. 
*/ 
public void handleMessage(Message msg)

 
/** 
* Handle system messages here. 
*/ 
public void dispatchMessage(Message msg)
if (msg.callback != null) { 
handleCallback(msg); 
} else
if (mCallback != null) { 
if (mCallback.handleMessage(msg)) { 
return


handleMessage(msg); 



首先在线程1新建一个handler,在线程2 新建一条消息msg,然后在线程2调用hander.sendMessage(msg),因为在handler的处理逻辑handleMessage()方法是放在线程1的,因此在线程2中调用了hander.sendMessage(msg),MessagQueue插入了这条消息,Looper发现有新消息,然后取出新消息,调用msg.target.dispatchMessage(msg),上面已经说到,target其实是hander,这样便成功地切换到线程1的handleMessage逻辑上来了。最常见的例子就是在Activity中声明一个Handler,然后异步线程去请求网络,再通过网络更新UI。可以参考这里。

Q2: 上面的模型中,为何不直接使用Handler来循环消息?

这个问题仁者见仁,智者见智,这只是一种实现消息循环的方法之一,而不是唯一方法。不是说一定要用Looper。

Q3: 需要注意的点?

使用Looper前一定要调用Looper.prepare生成线程内(TheadLocal存储)的Looper实例,然后才能调用Looper.loop()实现消息循环。下面是一个非主线程中Looper和Handler演示例子,此时整个线程是不会自动停止的,会一直阻塞,直到调用了Looper.quit()方法才会停止。(下面代码仅仅是为了演示,没有做任何事)

new Thread("example-1") { 
Handler handler = new Handler() { 
@Override 
public void handleMessage(Message msg)
// 自己的处理逻辑 
if (条件满足) { 
Looper.quit() ; //退出消息循环,结束线程 


} ; 
@Override 
public void run()
Looper.prepare(); 
// do something.... 
Message msg = Message.obtain(); 
msg.what = ... 
msg.obj = ... 
....... 
handler.sendMessage(msg); 
Looper.loop(); 

}.start() ; 

Q4: 为何ActivityThread中的Looper.loop()没有阻塞主线程?

。。。。。

参考

《Android开发艺术探索》

转载于:https://www.cnblogs.com/jasonkent27/p/5793225.html


最新回复(0)