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. HTTP常用端口号与对应的服务说明

    常用端口号与对应的服务以及端口关闭 端口简介:本文介绍端口的概念,分类,以及如何关闭/开启一个端口 21端口:21端口主要用于FTP(File Transfer Protocol,文件传输协议)服务. ...

  2. 基于SpringBoot的Environment源码理解实现分散配置

    前提 org.springframework.core.env.Environment是当前应用运行环境的公开接口,主要包括应用程序运行环境的两个关键方面:配置文件(profiles)和属性.Envi ...

  3. PHP过滤器

    这里介绍的过滤器包括: 1.filter_input 2.filter_input_array 3.filter_var 4.filter_var_array 5.filter_has_var 一.查 ...

  4. 使用Monkeyrunner进行Android自动化的总结

    http://www.2cto.com/kf/201411/356056.html 使用Monkeyrunner进行Android自动化的总结 使用Android自动化的方式,不仅可以用来对Andro ...

  5. cookie技术自动登录

    user public class User implements Serializable{ private String username; private String nick; privat ...

  6. C语言之指针基础概念

    今天就写一下关于C语言指针的一些感想吧. 很多同学都搞不懂指针,我一开始也云里雾里没看懂指针,而且老师又把指针说得很难的样子.其实主要是把指针”*“的作用给弄混了,不用畏惧,细心点看就可以了. 首先简 ...

  7. linuxubuntu升级.net core版本到2.0

    直接看这里 https://www.microsoft.com/net/core#linuxubuntu

  8. CENTOS 下安装APK反编译工具 APKTOOL

    转于:http://www.qiansw.com/centos-apk-apktool.html 我使用的是CentOS6.4 64位的系统.首先需要下载两个包.这里下载:https://code.g ...

  9. Spark缓存机制

    虽然默认情况下 RDD 的内容是临时的,但 Spark 提供了在 RDD 中持久化数据的机制.第一次调用动作并计算出 RDD 内容后,RDD 的内容可以存储在集群的内存或磁盘上.这样下一次需要调用依赖 ...

  10. DirectShow中写push模式的source filter流程 + 源码(内附具体凝视)

    尽管网上已有非常多关于DirectShow写source filter的资料.只是非常多刚開始学的朋友总说讲的不是非常清楚(可能当中作者省略了很多他觉得简 单的过程).读者总希望看到象第一步怎么做,第 ...