近日看到代码有view.post(runable),发现对handler机制又有些模糊,故做些复习。

这里就不再对具体的源码原理做深入复习了,就抄一些基本的结论吧。

1.基本概念

Message:基本的信息类

MessageQueue:Message队列,每个线程存储Message的容器

Looper:MessageQueue的管理者,每个线程只有一个,保存一直循环运转的状态,一旦发现MessageQueue中有Message就广播通知Handler进行处理

Handler:Message的发送者和处理者

2.基本功能原理

1、Looper.prepare()在本线程中保存一个Looper实例,然后该实例中保存一个MessageQueue对象;因为Looper.prepare()在一个线程中只能调用一次,所以MessageQueue在一个线程中只会存在一个。

2、Looper.loop()会让当前线程进入一个无限循环,不端从MessageQueue的实例中读取消息,然后回调msg.target.dispatchMessage(msg)方法。

Activity的启动代码中,已经在当前UI线程调用了Looper.prepare()和Looper.loop()方法。其他线程使用Looper需要显式调用Looper.prepare()和Looper.loop()方法。

3、Handler的构造方法,会首先得到当前线程中保存的Looper实例,进而与Looper实例中的MessageQueue想关联。

4、Handler的sendMessage方法,会给msg的target赋值为handler自身,然后加入MessageQueue中。

5、在构造Handler实例时,我们会重写handleMessage方法,也就是msg.target.dispatchMessage(msg)最终调用的方法。

6、Handler.post(runnable),其实依然调用的是Handler.sendMessage。而runnable中的逻辑可以理解为handerMessage中的流程

3.关于view.post(runnable)

View.post(runnable)可以理解成 New Handler(getMainLooper()).post(runnable)

其实就是让runable里的逻辑在主线程中执行即可。

4.最后抄几张图贴上来,以便记忆。

参考资料:http://blog.csdn.net/lmj623565791/article/details/38377229  Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系

Message,MessageQueue,Looper,Handler ——由view.post(runnable想到的)的更多相关文章

  1. Message,MessageQueue,Looper,Handler详解+实例

    Message,MessageQueue,Looper,Handler详解+实例 原文地址 Android的Handler使用(这篇简单介绍Handler的使用) 一.几个关键概念 1.Message ...

  2. Message,MessageQueue,Looper,Handler详解

    Message,MessageQueue,Looper,Handler详解   一.几个关键概念 1.MessageQueue:是一种数据结构,见名知义,就是一个消息队列,存放消息的地方.每一个线程最 ...

  3. [Android]Message,MessageQueue,Looper,Handler详解+实例

    转http://www.eoeandroid.com/forum-viewthread-tid-49595-highlight-looper.html 一.几个关键概念 1.MessageQueue: ...

  4. 深入源代码解析Android中的Handler,Message,MessageQueue,Looper

    本文主要是对Handler和消息循环的实现原理进行源代码分析.假设不熟悉Handler能够參见博文< Android中Handler的使用>,里面对Android为何以引入Handler机 ...

  5. 深入理解Message, MessageQueue, Handler和Looper

    做过Android的都知道Message, MessageQueue, Handler和Looper,但知道不代表你理解它们.有时觉得用得很顺手,但Android怎么实现又说不上来,总觉得似懂非懂.不 ...

  6. Android的消息机制: Message/MessageQueue/Handler/Looper

    概览   * Message:消息.消息里面可包含简单数据.Object和Bundle,还可以包含一个Runnable(实际上可看做回调). * MessageQueue:消息队列,供Looper线程 ...

  7. Android开发之漫漫长途 ⅥI——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  8. Android开发之漫漫长途 Ⅶ——Android消息机制(Looper Handler MessageQueue Message)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  9. Android 进阶14:源码解读 Android 消息机制( Message MessageQueue Handler Looper)

    不要心急,一点一点的进步才是最靠谱的. 读完本文你将了解: 前言 Message 如何获取一个消息 Messageobtain 消息的回收利用 MessageQueue MessageQueue 的属 ...

随机推荐

  1. Deferred

    http://blog.allenm.me/2012/01/jquery_deferred_promise_method/ http://www.ruanyifeng.com/blog/2011/08 ...

  2. STM32F10x 学习笔记6(USART实现串口通讯 2)

    这次讲讲利用串口收发中断来进行串口通讯.STM32 上为每个串口分配了一个中断.也就是说无论是发送完成还是收到数据或是数据溢出都产生同一个中断.程序需在中断处理函数中读取状态寄存器(USART_SR) ...

  3. android 如何进入某个具体的应用管理页面

    http://stackoverflow.com/questions/4421527/start-android-application-info-screen/4772481#4772481 pri ...

  4. dns智能解析对网站排名的影响

    网站排名是所有建站者都关系的问题,如何提升网站排名有很多因素,网站是否健康也与网站排名有关,下面智儒科技网站建设为你研究下如何判断自己的网站是否健康. 一般情况下,网站的排名在优化的基础上,怎么也上不 ...

  5. Lowest Common Ancestor in Binary Tree

    The problem: Given node P and node Q in a binary tree T. Find out the lowest common ancestor of the ...

  6. a trick in reading and storing file in the exact way!

    read and write file is a very common operation regarding file mainuplation. However, the powerfull g ...

  7. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  8. 工资表的生成、显示、修改工资,应用transactionscope 分布式事务

    一: 二:SalarySheetDAL.cs using System; using System.Collections.Generic; using System.Linq; using Syst ...

  9. Jump Game —— LeetCode

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  10. NHibernate遇到的问题集 持续更新。

    问题1: “NHibernate.TypeMismatchException”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: Provided id of t ...