Android Looper和Handler分析(2)
来源:未知 责任编辑:责任编辑 发表时间:2014-04-20 03:36 点击:次
}
sThreadLocal.set(new Looper());
}
/** Initialize the current thread as a looper, marking it as an application's main
* looper. The main looper for your application is created by the Android environment,
* so you should never need to call this function yourself.
* {@link #prepare()}
*/
//由framework设置的UI程序的主消息循环,注意,这个主消息循环是不会主动退出的
//
public static final void prepareMainLooper() {
prepare();
setMainLooper(myLooper());
//判断主消息循环是否能退出....
//通过quit函数向looper发出退出申请
if (Process.supportsProcesses()) {
myLooper().mQueue.mQuitAllowed = false;
}
}
private synchronized static void setMainLooper(Looper looper) {
mMainLooper = looper;
}
/** Returns the application's main looper, which lives in the main thread of the application.
*/
public synchronized static final Looper getMainLooper() {
return mMainLooper;
}
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
//消息循环,整个程序就在这里while了。
//这个是static函数喔!
public static final void loop() {
Looper me = myLooper();//从该线程中取出对应的looper对象
MessageQueue queue = me.mQueue;//取消息队列对象...
while (true) {
Message msg = queue.next(); // might block取消息队列中的一个待处理消息..
//if (!me.mRun) {//是否需要退出?mRun是个volatile变量,跨线程同步的,应该是有地方设置它。
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>