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)的更多相关文章

  1. Android 消息处理源代码分析(1)

    Android 消息处理源代码分析(1) 在Android中,通常被使用的消息队列的代码在文件夹\sources\android-22\android\os下,涉及到下面几个类文件 Handler.j ...

  2. Android HandlerThread 源代码分析

    HandlerThread 简单介绍: 我们知道Thread线程是一次性消费品,当Thread线程运行完一个耗时的任务之后.线程就会被自己主动销毁了.假设此时我又有一 个耗时任务须要运行,我们不得不又 ...

  3. Android HttpURLConnection源代码分析

    Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...

  4. Android KLog源代码分析

    Android KLog源代码分析 Android KLog源代码分析 代码结构 详细分析 BaseLog FileLog JsonLog XmlLog 核心文件KLogjava分析 遇到的问题 一直 ...

  5. android开发源代码分析--多个activity调用多个jni库的方法

    android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...

  6. Appium Android Bootstrap源代码分析之启动执行

    通过前面的两篇文章<Appium Android Bootstrap源代码分析之控件AndroidElement>和<Appium Android Bootstrap源代码分析之命令 ...

  7. Android AsyncTask 源代码分析

    AsyncTask源代码分析 public abstract class AsyncTask<Params, Progress, Result> { //日志TAG private sta ...

  8. Appium Android Bootstrap源代码分析之简单介绍

    在上一个系列中我们分析了UiAutomator的核心源代码,对UiAutomator是怎么执行的原理有了根本的了解.今天我们会開始另外一个在安卓平台上基于UiAutomator的新起之秀--Appiu ...

  9. [Android] Volley源代码分析(五岁以下儿童)Q \\ u0026一个

    Volley源代码分析系列那里一段时间,告诉我,有许多私人留言,同时一些问题抛出.对于一些简单的问题,我们跳,这两天被连接到朋友@smali提出的问题.告诉我你不得不赞叹查看源代码时的详细程度,大家一 ...

随机推荐

  1. ffmpeg代码解析

    void avdevice_register_all(void){    static int initialized;    if (initialized)        return;    i ...

  2. com.apple.installer.pagecontroller 错误 -1 pkg安装错误

    在网上下载了一个pkg 的安装文件: 在mac上安装一打就出现错误 原因是,文件从网上直接下载的,会出权限问题,需要修复安装软件的安装权限: 我的原因是,下载的是个rar的mac解压不了,就在线解压, ...

  3. Ubuntu12.04 Bugzilla 和 TestOpia的安装步骤

    1.        安装apache User@ubuntu:$  sudo apt-get install apache2 注:安装完以后能够通过http://192.168.128.128/ 来訪 ...

  4. 把IIS日志导入到数据库

    1.建表 CREATE TABLE [dbo].[inetlog0828]( [date] [date] NULL, ) NULL, ) NULL, ) NULL, ) NULL, ) NULL, [ ...

  5. 【VBA编程】14.操作工作簿对象

    [访问工作簿] 对已经打开的工作簿,可以通过使用索引号来访问工作簿,也可以通过名称来访问工作簿 [代码区域] Sub 访问工作簿() Dim counter As Integer counter = ...

  6. Hibernate 主配置文件详解

    摘要: 版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5595870.html 一.主配置文件命名规则 1.默认名称: ...

  7. Com与.Net互操作

    Com与.Net互操作 .Net调用Com组件主要分为两类:静态调用及动态调用.所谓静态调用:指通过tlbimp.exe命名产生Com组件在.Net环境下的包装类,然后通过这个包装类来访问Com组件. ...

  8. 给第三方dll加上强命名的方法[C#]

    在VS.NET 的命名行窗口下,输入如下的代码. 1 ,生成一个KeyFile sn -k keyPair.snk 2, 得到程序集的MSIL ildasm SomeAssembly.dll /out ...

  9. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  10. myeclipse中配置schemaLocation路径,实现xml文件自动提示

    在开发中,XML的xsi:schemaLocation路径都是指向网络,但是这个网络地址有时候很不给力导致工程检验XML格式缓慢.所以有必要再myeclipse中配置本地xsd文件路径,以免每次校验都 ...