android Handler机制详解

MessageQueue (消息队列):是looper中的一个消息队列;
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 方法介绍 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 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机制详解的更多相关文章
- Android Binder机制详解:手写IPC通信
想要掌握一样东西,最好的方式就是阅读理解它的源码.想要掌握Android Binder,最好的方式就是写一个AIDL文件,然后查看其生成的代码.本文的思路也是来自于此. 简介 Binder是Andro ...
- Android Download机制详解(一)DocumentUI部分
在Android中Google为我们集成了一套十分便利的Download机制,用来下载网络上的资源文件.以此省去了我们编写和维护大量与Download相关的代码. 组成 Android中Downloa ...
- [转]android Intent机制详解
转自:http://blog.csdn.net/t12x3456/article/details/7688154 1.什么是Intent Intent是一种运行时绑定(run-time binding ...
- android Intent机制详解
http://www.oschina.net/question/565065_67909 http://www.cnblogs.com/hummersofdie/archive/2011/02/12/ ...
- android binder机制详解
摘要 Binder是android中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经有2篇很好的文章了,2 ...
- android Handler机制之ThreadLocal详解
概述 我们在谈Handler机制的时候,其实也就是谈Handler.Message.Looper.MessageQueue之间的关系,对于其工作原理我们不做详解(Handler机制详解). Messa ...
- Android应用AsyncTask处理机制详解及源码分析
1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个知识点.前面我们分析了Handler异步机制原理(不了解的可以阅读我的<Android异步消息处理机 ...
- 【转载】Android应用AsyncTask处理机制详解及源码分析
[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 Android异步处理机制一直都是Android的一个核心,也是应用工程师面试的一个 ...
- Android事件传递机制详解及最新源码分析——ViewGroup篇
版权声明:本文出自汪磊的博客,转载请务必注明出处. 在上一篇<Android事件传递机制详解及最新源码分析--View篇>中,详细讲解了View事件的传递机制,没掌握或者掌握不扎实的小伙伴 ...
随机推荐
- Java白皮书的关键术语
“白皮书”可以在http://www.oracle.com/technetwork/java/langenv-140151.html上找到. 1.简单性. 2.面向对象. 3.网络技能(Network ...
- 关于subGradent descent和Proximal gradient descent的迭代速度
clc;clear; D=1000;N=10000;thre=10e-8;zeroRatio=0.6; X = randn(N,D); r=rand(1,D); r=sign(1-2*r).*(2+2 ...
- Windows转到linux中,文件乱码,文件编码转换 & 解决sqlplus连接oracle乱码
转载:http://www.cnblogs.com/wanyao/p/3399269.html 最近,学习又重新开始Linux学习,所以一直在Centos中,昨天一朋友把他在Windows下写的C程序 ...
- KBMMW 4.93.00 发布
可喜可敬,作者非常勤奋,跟上了delphi 10.1 的步伐. 4.93.00 April 26 2016 Important notes (changes that may break existi ...
- Linux和windows之间通过scp复制文件
Windows是不支持ssh协议的 需要安装WinSSHD 安装以及设置过程如下: BvSshServer(原名winsshd)官方下载页在这里:https://www.bitvise.com/dow ...
- shell 脚本杀死后台由php脚本控制运行的所有php脚本和java程序
效果: 运行命令: ./killallphpjavarm.sh java 源码: #!/bin/sh#根据进程名杀死进程#FileName: killjavaphprm.sh pgrep php ki ...
- MySQL分区表管理
RANGE,LIST分区管理 1:为未分区表创建分区 ALTER TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2; 2:删除某个分区的数据 ALTER TAB ...
- PHP判断请求是否是ajax请求
首先看一下框架里面是怎样判断的.ThinkPHP:define('IS_AJAX', ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && str ...
- 运行Java cmd程序 找不到或无法加载主类怎么解决
//这个问题原因有以下几种,但是和环境变量并没有太大的关系 //能够执行java 和 javac 就证明你的环境变量已经配置好了,其实 classpath 可以不配置 //假如有如下文件:H:\cod ...
- ZooKeeper设置ACL权限控制
ZK的节点有5种操作权限:CREATE.READ.WRITE.DELETE.ADMIN 也就是 增.删.改.查.管理权限,这5种权限简写为crwda(即:每个单词的首字符缩写)注:这5种权限中,del ...