public final class Looper;
官方的API:
Class used to run a message loop for a thread. Threads by default do not have a message loop associated with them; to create one, call prepare()
in the thread that is to run the loop, and then loop()
to have it process messages until the loop is stopped.
Most interaction with a message loop is through the Handler
class.
这个类用来运行为一个在线程中消息循环,线程默认是不和消息循环关联的。为了创建在Thread中可以对消息循环,调用prepare()
函数在线程中,然后调用loop()
来处理消息直到循环停止。
大部分和消息循环交互是通过Handler
这个类。
官方给出的文档,说明的不是很详细,如果我们深入去理解它,我们只能去挖掘它的内部源代码来分析。应为android是开源的嘛!首先我们先看下Looper这个类的内部代码:
public class Looper { private static final ThreadLocal sThreadLocal = new ThreadLocal(); //消息队列 final MessageQueue mQueue; // 当前线程 Thread mThread;
private Looper() {
mQueue = new MessageQueue();
mThread = Thread.currentThread(); } public static final void prepare() { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); }
sThreadLocal.set(new Looper()); }
public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}
public static final void loop() {
Looper me = myLooper(); //得到当前线程Looper
MessageQueue queue = me.mQueue; //得到当前looper对应的消息队列
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
// 开始循环
while (true) {
Message msg = queue.next();
if (msg != null) {
if (msg.target == null) {
// message的target为0,结束循环
return;
}
msg.target.dispatchMessage(msg); // 将消息交由message的target(其实就是handler)处理
msg.recycle();
}
}
}
public void quit() {
// 结束循环消息
Message msg = Message.obtain();
mQueue.enqueueMessage(msg, 0);
}
}
public class Handler {
final MessageQueue mQueue;
final Looper mLooper;
final Callback mCallback; //回调
public Handler() {
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 = null;
}
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0); //立即执行
}
private final Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
public boolean sendMessageAtTime(Message msg, long uptimeMillis)
{
boolean sent = false;
MessageQueue queue = mQueue;
if (queue != null) {
msg.target = this;
sent = queue.enqueueMessage(msg, uptimeMillis);
}
else {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w("Looper", e.getMessage(), e);
}
return sent;
}
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
private final void handleCallback(Message message) {
message.callback.run(); //执行线程run方法
}
public void handleMessage(Message msg) {
//处理消息,由客户端自己去实现调用
}
}
我们从Looper类,和Handler类的源代码,得到很多一些重要的结论:
Looper自己有一个消息队列,这个消息的添加由Handler来发送,然后,Looper自己不断的循环,取出消息,再有Handler的handleMessage方法来处理,但是handleMessage这个方法,是一个空实现,由程序员根据自己的业务逻辑来对消息进行处理。
Looper在android中被广泛的使用到;比如,在一个Activity里,大家知道activity 其实就是一个UI线程,在activity创建期间,它也创建了消息队列和消息循环Looper。这个大家可以到ActivityThread.java中能够找到对应的信息。
转载请注明http://www.cnblogs.com/yushengbo,否则将追究版权责任!