简单运行图:
  
名词解析:

Message(消息):定义了一个包含描述以及随意的数据对象可以被发送到Hanlder的消息,获得消息的最好方法是Message.obtain或者Handler.obtainMessage方法;

MessageQueue (消息队列):是looper中的一个消息队列;

Looper:用于使一个消息队列在线程中循环,线程中默认是没有消息队列循环的,创建方法demo:
  classLooperThreadextendsThread{
      publicHandler mHandler;       publicvoid run(){
          Looper.prepare();           mHandler =newHandler(){
              publicvoid handleMessage(Message msg){
                  // process incoming messages here
              }
          };           Looper.loop();
      }
  }

Looper方法
void dump(Printer pw, String prefix)
synchronized static Looper getMainLooper()
  获得主线程的Looper

Returns the application's main looper, which lives in the main thread of the application.
Thread getThread()

Return the Thread associated with this Looper.
static void loop()

Run the message queue in this thread.
static Looper myLooper()
获得当前线程关联的Looper

Return the Looper object associated with the current thread.
static MessageQueue myQueue()
获得当前线程关联的消息队列

Return the MessageQueue object associated with the current thread.
static void prepare()

Initialize the current thread as a looper.
static void prepareMainLooper()

Initialize the current thread as a looper, marking it as an application's main looper.
 
Handler(消息控制器):一个处理程序允许您发送或者处理消息以及与线程的MessageQueue相关联的可执行对象,每个Handler实例与单个线程以及该线程的消息队列想关联。当你创建一个新的Handler 实例的时候,该实例将被绑定到线程消息队列,该实例将传递messages消息或者可运行的对象到 message queue消息队列并且在当消息被获取的时候执行他们。
    Handler的两个主要用途:
  • 安排消息或者可执行对象在未来的某个时间点执行。
  • 将一个在子线程执行的动作放到消息队列中
处理消息通过 post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long), sendEmptyMessage(int),sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) 方法来完成。post系列方法允许你队列一个可执行对象,当消息被handler所在线程接收的时候会执行;sendMessage允许你队列一个包含一捆数据的Message对象,到消息handler所在线程接收的时候将调用handler实例的handleMessage(Message)方法,Massage 就是在另一个线程发送的消息。
    当你使用handler发送消息或者可执行对象的时候你可以选择handler所在线程获得消息就执行或延迟一定的时间再执行,当应用程序创建一个线程的时候,它的主线程专门运行一个message queue 消息队列用于维护应用程序顶级组件 。我们可以创建子线程通过Handler实例与主线程通信。
 
Handler 方法介绍
void dispatchMessage(Message msg)

Handle system messages here.
final void dump(Printer pw, String prefix)
final Looper getLooper()
String getMessageName(Message message)

Returns a string representing the name of the specified message.
void handleMessage(Message msg)
  子类必须实现这个方法来接收消息
final boolean hasMessages(int what, Object object)

Check if there are any pending posts of messages with code 'what' and whose obj is 'object' in the message queue.
final boolean hasMessages(int what)

Check if there are any pending posts of messages with code 'what' in the message queue.
final Message obtainMessage(int what, int arg1, int arg2)

Same as obtainMessage(), except that it also sets the what, arg1 and arg2 members of the returned Message.
final Message obtainMessage()

Returns a new Message from the global message pool.
final Message obtainMessage(int what, int arg1, int arg2, Object obj)

Same as obtainMessage(), except that it also sets the what, obj, arg1,and arg2 values on the returned Message.
final Message obtainMessage(int what)

Same as obtainMessage(), except that it also sets the what member of the returned Message.
final Message obtainMessage(int what, Object obj)

Same as obtainMessage(), except that it also sets the what and obj members of the returned Message.
final boolean post(Runnable r)
   使可执行的r被添加到消息队列

Causes the Runnable r to be added to the message queue.
final boolean postAtFrontOfQueue(Runnable r)

Posts a message to an object that implements Runnable.
final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
    在规定的时间点执行

Causes the Runnable r to be added to the message queue, to be run at a specific time given by uptimeMillis.
final boolean postAtTime(Runnable r, long uptimeMillis)

Causes the Runnable r to be added to the message queue, to be run at a specific time given by uptimeMillis.
final boolean postDelayed(Runnable r, long delayMillis)
延迟执行规定的时间长度

Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.
final void removeCallbacks(Runnable r)

Remove any pending posts of Runnable r that are in the message queue.
final void removeCallbacks(Runnable r, Object token)

Remove any pending posts of Runnable r with Object token that are in the message queue.
final void removeCallbacksAndMessages(Object token)

Remove any pending posts of callbacks and sent messages whose obj is token.
final void removeMessages(int what)

Remove any pending posts of messages with code 'what' that are in the message queue.
final void removeMessages(int what, Object object)

Remove any pending posts of messages with code 'what' and whose obj is 'object' that are in the message queue.
final boolean sendEmptyMessage(int what)

Sends a Message containing only the what value.
final boolean sendEmptyMessageAtTime(int what, long uptimeMillis)

Sends a Message containing only the what value, to be delivered at a specific time.
final boolean sendEmptyMessageDelayed(int what, long delayMillis)

Sends a Message containing only the what value, to be delivered after the specified amount of time elapses.
final boolean sendMessage(Message msg)
将一个消息添加到消息队列的末尾

Pushes a message onto the end of the message queue after all pending messages before the current time.
final boolean sendMessageAtFrontOfQueue(Message msg)
将一个消息添加到消息队列的最前面

Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop.
boolean sendMessageAtTime(Message msg, long uptimeMillis)
在规定的时间点添加消息到队列

Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds) uptimeMillis.
final boolean sendMessageDelayed(Message msg, long delayMillis)
延迟规定的时间再消息添加到消息队列

Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
String toString()

Returns a string containing a concise, human-readable description of this object.
 
接触不深,请高手指点,持续更新!
 
 
 
 
 
 
 

android Handler机制详解的更多相关文章

  1. Android Binder机制详解:手写IPC通信

    想要掌握一样东西,最好的方式就是阅读理解它的源码.想要掌握Android Binder,最好的方式就是写一个AIDL文件,然后查看其生成的代码.本文的思路也是来自于此. 简介 Binder是Andro ...

  2. Android Download机制详解(一)DocumentUI部分

    在Android中Google为我们集成了一套十分便利的Download机制,用来下载网络上的资源文件.以此省去了我们编写和维护大量与Download相关的代码. 组成 Android中Downloa ...

  3. [转]android Intent机制详解

    转自:http://blog.csdn.net/t12x3456/article/details/7688154 1.什么是Intent Intent是一种运行时绑定(run-time binding ...

  4. android Intent机制详解

    http://www.oschina.net/question/565065_67909 http://www.cnblogs.com/hummersofdie/archive/2011/02/12/ ...

  5. android binder机制详解

    摘要 Binder是android中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经有2篇很好的文章了,2 ...

  6. android Handler机制之ThreadLocal详解

    概述 我们在谈Handler机制的时候,其实也就是谈Handler.Message.Looper.MessageQueue之间的关系,对于其工作原理我们不做详解(Handler机制详解). Messa ...

  7. Android应用AsyncTask处理机制详解及源码分析

    1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个知识点.前面我们分析了Handler异步机制原理(不了解的可以阅读我的<Android异步消息处理机 ...

  8. 【转载】Android应用AsyncTask处理机制详解及源码分析

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个 ...

  9. Android事件传递机制详解及最新源码分析——ViewGroup篇

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇<Android事件传递机制详解及最新源码分析--View篇>中,详细讲解了View事件的传递机制,没掌握或者掌握不扎实的小伙伴 ...

随机推荐

  1. J2EE应用监控后台执行SQL

    我们可能已经很熟悉在未使用数据库连接池的hibernate的环境下,配置p6spy和sql profiler.这在单独使用hibernate,以及项目初期是有效的.但是,在真实的开发环境下,往往是项目 ...

  2. SQL Server附加数据库出现错误5123的正确解决方法

    因为自己有一本基于SQL Server 2005的数据库教程,里边使用的示例数据库是AdventureWorks for SQL Server 2005,而我的机子上装的是SQL Server 200 ...

  3. 用CMake构建Qt5的Visual Studio工程

    使用Visual Studio构建Qt工程的方法有很多种,可以使用Visual Studio自带的功能手动创建配置工程,也可以创建pro文件,然后通过VS的Qt插件导入进行创建.还有一种方式是通过CM ...

  4. rabbitMQ学习(五)

    topic匹配模式,topic能满足匹配结果就行. 发送端: public class EmitLogTopic { private static final String EXCHANGE_NAME ...

  5. MSSQL订阅库索引对齐

    需求如下图: 在原来的架构中是每台web服务器都固定访问某一台数据库服务器,所以就造成了每台数据库订阅服务器上的索引不一致.现在的需求就是要把所有的订阅库上的索引调整为一致,为了就是实现高可用+负载均 ...

  6. 跨过几个坑,终于完成了我的第一个Xamarin Android App!

    时间过得真快,距离上次发随笔又是一年多.作为上次发的我的第一个WP8.1应用总结的后继,这次同样的主要功能,改为实现安卓版APP.前几个月巨硬收购Xamarin,把Xamarin集成到VS里了,大大方 ...

  7. Spark-Mllib(二)基本统计

    一.基本统计量 统计向量的长度,最大值,最小值,非0个数,模1和模2,方差等 import org.apache.spark.mllib.linalg.{Vector,Vectors} import ...

  8. HDU 5410 CRB and His Birthday ——(完全背包变形)

    对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: #include <stdio.h> #include <algorithm& ...

  9. 数据库一些常用的SQL语句

    1.多表连接查询: 假设现在有三个表,One,Two,Three: One表字段:Code(主键),Name Two表字段:Birthday,T_code(One表Code的外键) Three表字段: ...

  10. CU上看到的一个简单的算法帖子

    今天也是明白了,编程与数学的关系.例子很简单,不过能说明问题. 如果我们优化算法只从计算机特性来考虑,那么我们的人脑也成了计算机.不要忘记数学对于算法的重要影响. 题目: 返回小于数字 N 的所有 3 ...