Android 消息处理源代码分析(2)
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);
}
...
}
Android 消息处理源代码分析(2)的更多相关文章
- Android 消息处理源代码分析(1)
Android 消息处理源代码分析(1) 在Android中,通常被使用的消息队列的代码在文件夹\sources\android-22\android\os下,涉及到下面几个类文件 Handler.j ...
- Android HandlerThread 源代码分析
HandlerThread 简单介绍: 我们知道Thread线程是一次性消费品,当Thread线程运行完一个耗时的任务之后.线程就会被自己主动销毁了.假设此时我又有一 个耗时任务须要运行,我们不得不又 ...
- Android HttpURLConnection源代码分析
Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...
- Android KLog源代码分析
Android KLog源代码分析 Android KLog源代码分析 代码结构 详细分析 BaseLog FileLog JsonLog XmlLog 核心文件KLogjava分析 遇到的问题 一直 ...
- android开发源代码分析--多个activity调用多个jni库的方法
android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...
- Appium Android Bootstrap源代码分析之启动执行
通过前面的两篇文章<Appium Android Bootstrap源代码分析之控件AndroidElement>和<Appium Android Bootstrap源代码分析之命令 ...
- Android AsyncTask 源代码分析
AsyncTask源代码分析 public abstract class AsyncTask<Params, Progress, Result> { //日志TAG private sta ...
- Appium Android Bootstrap源代码分析之简单介绍
在上一个系列中我们分析了UiAutomator的核心源代码,对UiAutomator是怎么执行的原理有了根本的了解.今天我们会開始另外一个在安卓平台上基于UiAutomator的新起之秀--Appiu ...
- [Android] Volley源代码分析(五岁以下儿童)Q \\ u0026一个
Volley源代码分析系列那里一段时间,告诉我,有许多私人留言,同时一些问题抛出.对于一些简单的问题,我们跳,这两天被连接到朋友@smali提出的问题.告诉我你不得不赞叹查看源代码时的详细程度,大家一 ...
随机推荐
- java集合Collection接口
collection集合 Map集合 Hashtable和HashMap的区别: Hashtable的方法是同步的,而HashMap的方法不是.HashMap可以将空值作为一个表的条目的key或val ...
- Python中的__name__
python中if __name__ == '__main__': 的解析 经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用. 模块是对象,并且 ...
- XML的SelectNodes使用方法以及XPath(转)
XPath 是 XML 的内容,这里 SelectNodes 是 C# 中 XmlDocument 或 XmlNode 的一个方法.SelectNodes 使用 XPath 来选取节点. 重要语法 S ...
- lsb_release: command not found 解决
问题:lsb_release 是查看系统版本信息的工具 [root@localhost ~]# lsb_release -a-bash: lsb_release: command not found ...
- Python-绑定与未绑定方法通俗讲解
像函数一样,Python中的类方法也是一种对象.由于既可以通过实例也可以通过类来访问方法,所以在Python里有两种风格: 未绑定的类方法:没有self 通过类来引用方法返回一个未绑定方法 ...
- java基础讲解07-----数组
1.什么是数组 2.怎么使用数组 package test; public class ShuZu { public static void main(String[] args ...
- Excel 时间格式相减
https://jingyan.baidu.com/article/3065b3b6e8b9dabecff8a4d6.html datedif函数是excel的隐藏函数,主要用于计算日期之差,不是四舍 ...
- python selenium --命令之文字范本匹配
文字范本匹配 ======================================= 文字范本匹配其实可以理解为通配符.我想大家都用过windows 系统自带的搜索功能. * 星号代表一个 ...
- hihoCoder[Offer收割]编程练习赛1题目解析
题目1 : 九宫 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi近期在教邻居家的小朋友小学奥数.而近期正好讲述到了三阶幻方这个部分,三阶幻方指的是将1~9不反 ...
- PHP的CURLOPT_POSTFIELDS参数使用数组和字符串的区别
手册上解释: CURLOPT_POSTFIELDS 全部数据使用HTTP协议中的"POST"操作来发送.要发送文件,在文件名前面加上@前缀并使用完整路径.这个参数可以通过urle ...