您的位置:首页 > 移动开发 > Android开发

安卓android.os.Looper.java分析

2016-01-07 23:05 477 查看
looper对象是要是一个消息处理的循环,使用的场景是,附着在一个线程里面进行消息处理。常用的方法也是准备消息队列,进入消息处理循环,操作消息队列。

准备方法:prepare

使用looper的第一步,必须调用prepare方法,创建出looper对象

有几种prepare方法,looper创建出来后,通过sThreadLocal保存起来。

一种可以指定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));
}


一种允许推出的looper

public static void prepare() {
prepare(true);
}


一种不允许推出的looper

public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}


构造方法

构造方法中,初始化一个消息队列。这个是loop主要打交道的对象。

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


loop方法,主要干活的方法

这个方法在状态机分析时,已经遇到过,死循环,主要是从消息队列中获取消息,然后处理。当消息队列中返回没有消息时,意味着队列已经销毁,循环推出。

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.recycle();
}
}


*其他方法*

大多数looper类的方法都是给予messagequeue的方法来来封装,无需再重复分析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android Looper