Android的消息机制源码分析(1)- ThreadLocal
Android的消息机制源码分析(2)- 消息队列(MessageQueue)
Android的消息机制源码分析(3)- Looper
Android的消息机制源码分析(4)- Handler
Android的消息机制的总体流程
Handler发送消息工作流程
主线程中的消息循环
这里有点不一样的是,用了prepareMainLooper()给主线程创建Looper,
创建好了之后它的Handler是在ActivityThread内部
private class H extends Handler {
public static final int LAUNCH_ACTIVITY = 100;
public static final int PAUSE_ACTIVITY = 101;
public static final int PAUSE_ACTIVITY_FINISHING= 102;
public static final int STOP_ACTIVITY_SHOW = 103;
public static final int STOP_ACTIVITY_HIDE = 104;
public static final int SHOW_WINDOW = 105;
public static final int HIDE_WINDOW = 106;
public static final int RESUME_ACTIVITY = 107;
public static final int SEND_RESULT = 108;
public static final int DESTROY_ACTIVITY = 109;
public static final int BIND_APPLICATION = 110;
public static final int EXIT_APPLICATION = 111;
public static final int NEW_INTENT = 112;
public static final int RECEIVER = 113;
public static final int CREATE_SERVICE = 114;
public static final int SERVICE_ARGS = 115;
public static final int STOP_SERVICE = 116;
}
可以看到有好多消息的类型常量,
消息模型
ActivityThread通过Application和AMS进行线程间通信,AMS通过线程间通信的方式完成主线程的请求后回调ApplicationThread中Binder方法,然后ApplicationThread会向H发型消息,H收到消息会切换到主线程中执行
注意点:
ActivityThread不是线程,应该是封装了UI消息循环和操作Activity生命周期的工具类