Android中的消息机制


前言

Android中的消息机制是指线程之间的通信机制。我们都知道,如果我们在UI主线程中做耗时的操作而无法及时处理时,程序会弹出ANR全名Application Not Responding, 也就是”应用无响应”的对话框。

首先来一张图,从整体上来看一下android消息机制。

view继承关系

Handler:用于发送消息和处理消息
MessageQueue: 一个先进先出的消息队列
Looper:循环者,它不断的循环的遍历查询消息队列

Looper中会创建一个消息队列,并进入消息循环,不断的从消息队列中取出消息,然后分发消息给对应的消息处理函数,如果消息队列为空,它会进入阻塞等待,直到有新的消息到来,然后被唤醒。

源码分析

Looper

1
2
3
4
5
6
 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));
}

这就是Looper的创建函数,它创建了一个Looper实例并放到ThreadLocal中。
ThreadLocal是一个线程共享和线程安全的,ThreadLocal变量在不同的线程中有不同的副本。

这里,首先检查线程是否有Looper,如果已经有,就报”Only one Looper may be created per thread”异常。也就是说一个线程只能有一个Looper,不能重复创建。

进入Looper的构造函数

1
2
3
4
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}

Looper的构造函数中主要是创建了一个消息队列,和赋值当前线程变量。

开启消息循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}

final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;

final long traceTag = me.mTraceTag;
if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
}
final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
final long end;
try {
//将消息分发给对应的handler处理
msg.target.dispatchMessage(msg);
end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
} finally {
if (traceTag != 0) {
Trace.traceEnd(traceTag);
}
}
if (slowDispatchThresholdMs > 0) {
final long time = end - start;
if (time > slowDispatchThresholdMs) {
Slog.w(TAG, "Dispatch took " + time + "ms on "
+ Thread.currentThread().getName() + ", h=" +
msg.target + " cb=" + msg.callback + " msg=" + msg.what);
}
}

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方法的工作,在代码中已经进行注释说明。

Looper#loop中会将消息分发给对应的handler处理。

1
msg.target.dispatchMessage(msg);

现在我们进入handler。

Handler

1
Handler handler = new Handler(Looper.myLooper());

首先看Handler的构造函数,可以知道Handler是怎么和Looper取得关联的。

1
2
3
4
5
6
7
public Handler(Looper looper, Callback callback, boolean async) 
{
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
mAsynchronous = async;
}

主要为Handler的四个变量赋值,其中确定了Handler是和哪一个Looper关联,和Handler发送消息到对应的哪个消息队列。可以知道,一个Handler只有一个Looper和对应的MessageQueue。而不同的Handler可以共享同一个Looper和MessageQueue,这就看你在初始化Handler时与哪个Looper关联了。

Handler无参数的构造函数是和哪个Looper关联呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Handler() {
this(null, false);
}

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无参数的构造函数仍然主要是为那四个变量赋值。它会首先取出当前线程的消息循环者,如果线程没有循环者,会报一个异常。

发送消息到循环队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}

public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
return false;
}
return enqueueMessage(queue, msg, uptimeMillis);
}

sendMessage中会调用sendMessageDelayed,sendMessageDelayed再调用sendMessageAtTime,最后会调用enqueueMessage将消息入队。post开头的方法是调用相应send开头的方法的。

进入Handler#enqueueMessage

1
2
3
4
5
6
7
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}

在分析Looper#loop时,其中有将消息的分发给相应的Handler处理的逻辑,而正是在第2行代码时,它们取得联系的。然后将消息放入Handler关联的Looper中的消息队列。

在MessageQueue#enqueueMessage中,消息入队时,如果消息队列是阻塞休眠状态,会唤醒消息队列。

1
2
3
4
5
6
if (p == null || when == 0 || when < p.when) {
// New head, wake up the event queue if blocked.
msg.next = p;
mMessages = msg;
needWake = mBlocked;
}

在Looper#loop中,会将消息分发给对应的Handler处理函数dispatchMessage处理

1
msg.target.dispatchMessage(msg);

进入Handler#dispatchMessage

1
2
3
4
5
6
7
8
9
10
11
12
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}

java.lang.Callback

1
2
3
public interface Runnable {
public abstract void run();
}

Handler#Callback

1
2
3
public interface Callback {
public boolean handleMessage(Message msg);
}

优先调用Message的callback接口,如果Handler有Callback,调用Callback,否则会调用handleMessage方法。

Handler#handleMessage

1
2
public void handleMessage(Message msg) {
}

这是一个空方法,具体的消息逻辑由我们自己定义。

到此,这个流程已经解释完毕

后话

在非UI线程中只要找好时机也是可以更新UI的。这个会在源码再分析。

坚持原创技术分享,您的支持将鼓励我继续创作!
-------------本文结束感谢您的阅读-------------