public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}

首先到Handler运行过程的总结:

1、 Looper.prepare()方法

        为当前线程绑定looper。

        在looper构造方法中创建一个messageQueue



    2、 创建handler 重并写handleMessage方法



    3、 使用handler发送消息,终于消息都会发送至messageQueue对象中。在messageQueue其中,全部的message按应该运行的时间的先后顺序,从小到大排列



    4、Looper.loop()

        在此方法中。开启死循环,不断的从messageQueue中取出应该运行的message,并运行message 相应的handler中的dispatchMessage方法。即。运行我们重写的handleMessage方法

參照以上分析在子线程中创建Handler对象:

	new Thread(){
@Override
public void run() {
Message msg = Message.obtain();
Looper.prepare();//若没有调用此方法则抛出异常 Can't create handler inside thread that has not called Looper.prepare()
Handler handler2 = new Handler(){
public void handleMessage(Message msg) {
Toast.makeText(MainActivity.this, "收到子线程message消息", 0).show();
};
};
handler2.sendMessage(msg);
Looper.loop();
}
}.start();

对照在主线程中创建Handler实例对象我们发现,在子线程中创建Handler对象须要在创建前调用Looper.prepare()方法在创建后调用Looper.loop方法,那到底这两个方法是做什么的呢?

先看看系统的Looper.prepare方法:

public static final void prepare() {

        if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
// 为当前线程绑定一个looper对象,以sThreadLocal为key
sThreadLocal.set(new Looper());
}

即:调用Looper.prepare方法时为当前线程绑定了一个Looper对象,所以Looper.prepare方法仅仅能调用一次。即一个线程仅仅能有一个Looper对象

再看看Looper的构造方法:

 private Looper() {
mQueue = new MessageQueue();
}

由于一个线程仅仅能有一个Looper对象,所以一个线程也仅仅能有一个MessageQueue对象

先让我们看看Handler的构造方法:

public Handler() {
//获得当前线程的looper对象
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
// 获得looper中MessageQueue的引用
mQueue = mLooper.mQueue;
}

再看看系统的Looper.myLooper方法:即获取调用Looper.prepare方法时保存在sThreadLoad的Looper对象,所以Looper.prepare方法要在new Handler方法前调用

    public static final Looper myLooper() {
return (Looper)sThreadLocal.get();
}

即:当创建Handler时会先调用Looper.myLooper()方法获取当前线程的Looper对象,假设Looper==null,则抛出异常

通过以上两个方法,当前线程的唯一Looper对象和MessageQueue对象都已创建,接下来该sendMessage了

查看系统源代码可知:sendEmptyMessage等。发送信息的方法,终于都是调用了SendMessageAtTime(msg,when);

而SendMessageAtTime(msg,when);方法终于的目的就是为了queue.enqueueMessage(msg, uptimeMillis);。当中msg为发送的Message对象,uptimeMillis为SystemClock.uptimeMillis() + when

查看系统的enqueueMessage方法,该方法终于实如今messageQueue其中。全部的message按运行的先后顺序,从小到大排列

final boolean enqueueMessage(Message msg, long when) {

            msg.when = when; // 将运行时间设置给msg.when
Message p = mMessages; // 定义变量p = mMessage ,mMessage初终指向对列的第一个Message 对象 if (p == null || when == 0 || when < p.when) {
// 当队列中为空的时候,mMessage = msg
msg.next = p;
mMessages = msg;
this.notify();
} else {
// 否则将要进入队列的msg的运行时间和队列中的message的运行时间进行比較,
// 终于会使messageQueue中的全部的message按时间为顺序从小到大排列
// 即按运行的先后顺序排列
Message prev = null;
while (p != null && p.when <= when) {
prev = p;
p = p.next;
}
msg.next = prev.next;
prev.next = msg;
this.notify();
}
}

消息发送成功这时候该调用Looper.loop方法:即完毕了从MessageQueue中取出须要运行的Message。并运行我们重写的handlMessage方法

 public static final void loop() {
//获得当前线程的looper对象及messageQueue对象
Looper me = myLooper();
MessageQueue queue = me.mQueue; //开启while(true)循环
while (true) {
//从消息队列中取出一个message,假设message运行时间不到。那就wait等一会
Message msg = queue.next(); // might block //运行message 相应的handler中的dispatchMessage方法。即。运行我们重写的handleMessage方法
msg.target.dispatchMessage(msg); }
}
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Handler消息源代码分析的更多相关文章

  1. Memcached源代码分析 - Memcached源代码分析之消息回应(3)

    文章列表: <Memcached源代码分析 - Memcached源代码分析之基于Libevent的网络模型(1)> <Memcached源代码分析 - Memcached源代码分析 ...

  2. handler 源代码分析

    handler Looper 轮询器 MessageQueue 消息对象 1 主线程在一创建的时候就会调用, public static void prepareMainLooper() {}构造方法 ...

  3. RTMPdump(libRTMP) 源代码分析 10: 处理各种消息(Message)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  4. RTMPdump(libRTMP) 源代码分析 9: 接收消息(Message)(接收视音频数据)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  5. RTMPdump(libRTMP) 源代码分析 8: 发送消息(Message)

    ===================================================== RTMPdump(libRTMP) 源代码分析系列文章: RTMPdump 源代码分析 1: ...

  6. hostapd源代码分析(二):hostapd的工作机制

    [转]hostapd源代码分析(二):hostapd的工作机制 原文链接:http://blog.csdn.net/qq_21949217/article/details/46004433 在我的上一 ...

  7. Hadoop源代码分析

    http://wenku.baidu.com/link?url=R-QoZXhc918qoO0BX6eXI9_uPU75whF62vFFUBIR-7c5XAYUVxDRX5Rs6QZR9hrBnUdM ...

  8. Android系统默认Home应用程序(Launcher)的启动过程源代码分析

    在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还需要有一个 Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home ...

  9. Android应用程序进程启动过程的源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址: http://blog.csdn.net/luoshengyang/article/details/6747696 Android 应用程序框架层创 ...

随机推荐

  1. 【WinRT】【译】【加工】在 XAML 中制作圆形图片

    原文:[WinRT][译][加工]在 XAML 中制作圆形图片 原文地址:http://timheuer.com/blog/archive/2015/05/06/making-circular-ima ...

  2. C语言 - 结构体(struct)比特字段(:) 详细解释

    结构体(struct)比特字段(:) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26722511 结构体(struc ...

  3. C#获取Excel中所有的Sheet名称

    原文地址:http://blog.csdn.net/qq1010726055/article/details/6858849 Excel.Application myExcel = new Excel ...

  4. SE 2014年4月8日

    1.路由引入的作用? 当网络中运行多种路由协议的时候,由于不同协议的路由算法和度量值等均不相同,路由引入可以将不同协议的路由引入到当前的路由协议中,保证网络的互通. 对比单向入和双向入 单向引入是只将 ...

  5. WCF之添加自定义用户名密码认证

    1.创建WCF服务应用以及调用客户端(请自行google).  2.创建X509证书       cmd 进入  C:\Program Files\Microsoft SDKs\Windows\v6. ...

  6. Java中的statickeyword具体解释

    1.statickeyword主要有2个作用: ①为某特定的数据类型或者对象分配单一的存储空间.而与创建对象的个数无关. ②在不创建对象的情况下能够直接通过类名来直接调用方法或者使用类的属性. 2.s ...

  7. HGE基础教程

    作者:寰子 来源:http://www.hgechina.com/前言: 写道: 无意中发现了hge中文社区,听朋友介绍,认识了hge,然后开始对它进行研究,并使用hge开始制作游戏. 因为我所得的资 ...

  8. Android Ant打包笔记

    本文文档的下载地址(Word版):http://download.csdn.net/detail/yangwei19680827/7250711 Android Ant 打包 网上找了ant打包的资料 ...

  9. freemarker导出word带图片

    导出word带图片 如果你需要在word中添加图片,那你就在第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码: <w:binData w:nam ...

  10. Wake-On-LAN待机或休眠模式中唤醒

    Wake-On-LAN简称WOL,是一种电源管理功能:如果存在网络活动,则允许设备将操作系统从待机或休眠模式中唤醒.许多主板厂商支持IBM提出的网络唤醒标准.该标准允许网络管理员远程打开PC机电源,以 ...