File tree Expand file tree Collapse file tree 1 file changed +2
-2
lines changed
OperatingSystem/AndroidKernal Expand file tree Collapse file tree 1 file changed +2
-2
lines changed Original file line number Diff line number Diff line change @@ -179,7 +179,7 @@ private Looper(boolean quitAllowed) {
179179 mThread = Thread . currentThread();
180180}
181181```
182- 从代码上可以看到prepare()方法的作用是创建一个Looper对象,然后将该Looper对象保存到sThreadLocal中。
182+ 从代码上可以看到prepare()方法的作用是创建一个Looper对象,然后将该Looper对象保存到sThreadLocal中。也就是说,prepare函数通过ThreadLocal机制,巧妙地把Looper和调用prepare的线程(也就是最终的处理线程)绑定在一起了。当事件源向这个Looper发送消息的时候,其实是把消息加到这个Looper的消息队列里了。那么,该消息就将由和Looper绑定的处理线程来处理。
183183
184184这里有点麻烦了,我们上面使用的代码中并没有调用Looper.prepare()方法啊,理论上这里应该是null,Handler是无法使用的,为什么我们还能正常使用Handler?Looper.prepare()究竟是什么时候调用的?那我们需要看一下prepare()和prepare(boolean quitAllowed)方法都有哪些地方调用了:
185185
@@ -921,4 +921,4 @@ public class MyActivity extends Activity {
921921---
922922
923923- 邮箱 :charon.chui@gmail.com
924- - Good Luck!
924+ - Good Luck!
You can’t perform that action at this time.
0 commit comments