Android自助餐Handler消息机制完全解析(四)Looper解析
Looper
如果你搞过Arduino,那么你肯定知道这个loop()
方法。没接触过也没关系,这个方法就是一个通过死循环来重复做某件事的方法。区别是Arduion的循环控制在loop()
方法外,而Looper
的循环控制在loop()
方法内。这个Looper
类本身则是对这个方法做了一些封装。
初始化prepare()
这里标题不是构造方法,因为其构造方法被private
修饰,那么来看看它什么时候调用了构造方法。很容易就能找到private static void prepare(boolean quitAllowed)
方法,该类仅在这里调用了构造方法,然而这个prepare()
也是被private
修饰的,那么来看看这个带参的prepare()
又在哪里被调用了。结果可以找到两个public static
修饰的方法:prepare()
和prepareMainLooper()
。
第一个方法原文说明如下:
Initialize the current thread as a looper.This gives you a chance to create handlers that then reference this looper, before actually starting the loop. Be sure to call
loop()
after calling this method, and end it by callingquit()
.
某以不才为诸君翻译如下:
作为looper初始化当前线程。提供一个机会来创建handler并使用looper。在使用之前,请在此方法之后调用
loop()
,并在结束时调用quit()
。
第二个方法原文说明如下:
Initialize the current thread as a looper, mark