Android 消息处理源代码分析(2)
2024-08-26 08:47:29
Android 消息处理源代码分析(1)点击打开链接
继续接着分析剩下的类文件
Looper.java
public final class Looper {
final MessageQueue mQueue; //消息队列
final Thread mThread; //Looper联系的线程 public static void prepare() {
prepare(true);
} private static void prepare(boolean quitAllowed) { //先会检查是否有Looper。若有则抛出异常。没有的话则创建一个Looper实例保存起来
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed));
} public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
//在这个线程中执行消息队列。调用quit()停止
public static void loop() {
...
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(); // 从消息队列中取出一条消息
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); //交给msg的handler分发消息处理 ...
}
//取出当前线程的Looper,返回空则表示当前线程没有Looper
public static Looper myLooper() {
return sThreadLocal.get();
}
}
Handler.java
public class Handler {
//定义Callback接口
public interface Callback {
public boolean handleMessage(Message msg);
} //子类要实现的消息处理方法
public void handleMessage(Message msg) {
} * Handle system messages here.
*/
public void dispatchMessage(Message msg) { //分发信息
if (msg.callback != null) { //若指定了msg.callback,则由它处理
handleCallback(msg);
} else {
if (mCallback != null) { //若指定了Handler.mCallback。则由它处理
if (mCallback.handleMessage(msg)) { //调用mCallback接口的实现方法
return;
}
}
handleMessage(msg); 最后才调用Handler自身重载的handleMessage方法
}
}
分发消息函数中,消息先会检查自身有没有处理自身的回调Runnable,若有则由它处理。若没有则会检查该handler有无自身的回调处理,若有则调用,若没有则调用自身重载的handleMessage方法 //Handler的生成总是和它当前所处线程有关的,假设当前线程中没有一个Looper。则会报错。UI线程中默认有产生Looper的函数
public Handler() {
this(null, false);
} //使用指定的Looper(能够处理那个Looper线程中的消息)。不用默认的从当前线程中取出Looper
public Handler(Looper looper) {
this(looper, null, false);
}
...
}
最新文章
- SSH加固
- Bash 里的转义序列
- 作弊Q-百威
- UGUI与DOtween的坑
- python 中md5 和 sha1 加密, md5 + os.urandom 生成全局唯一ID
- HTML5之创新的视频拼图剖析式学习之二
- 如何在Linux中使用rz/sz工具进行文件传输
- Statement及PreparedStatement执行多个sql
- linux 系统安装 mysql
- 第3章 rpm命令管理
- 剑指offer--面试题14--收获
- MVC和WebForm的优缺点对比
- 删除链表的倒数第N个节点
- Java 学习(1) ---JDK安装和配置环境变量
- window10安装黑苹果
- CSS 基础 优先级 选择器 继承
- 005_ss-link.info的ping探测工具
- MATLAB 的函数句柄
- mysql 中只能使用 localhost 登录,用ip不能登陆
- UI5-文档-4.18-Icons
热门文章
- 屏幕录像大师如何把LXE文件转换为EXE文件
- super.getclass()的结果是父类还是子类?
- vue - helloWorld
- OpenERP|ODOO高德地图应用
- CentOS安装glibc-2.14(转)
- 【微信小程序】日历插件,适用于酒店订房类小程序
- shell脚本中执行mysql 语句,去除warning using a password on the command line interface can be insecure信息
- Android逆向之旅---SO(ELF)文件格式详解(转)
- 阿里云web播放器
- weblogic stuck实验2014-11-14