从源码与官方文档看之Handler篇(一)

简介: 好家伙,写了四篇文章才发现自己看源码的Handle所属的包不一样

前言

好家伙,写了四篇文章才发现自己看源码的Handle所属的包不一样,我们安卓常用的是android.os.Handler,而我之前写的文章看的源码都是java.util.logging.Handler,真的离了大谱。所以从现在起我们开始看真正Android的Handler(我就说我半天一个熟悉的成员变量和属性都没看到影)。

正篇

老规矩,先看官方给Handler的注释:

/**
 * A Handler allows you to send and process {@link Message} and Runnable
 * objects associated with a thread's {@link MessageQueue}.  Each Handler
 * instance is associated with a single thread and that thread's message
 * queue. When you create a new Handler it is bound to a {@link Looper}.
 * It will deliver messages and runnables to that Looper's message
 * queue and execute them on that Looper's thread.
 *
 * <p>There are two main uses for a Handler: (1) to schedule messages and
 * runnables to be executed at some point in the future; and (2) to enqueue
 * an action to be performed on a different thread than your own.
 *
 * <p>Scheduling messages is accomplished with the
 * {@link #post}, {@link #postAtTime(Runnable, long)},
 * {@link #postDelayed}, {@link #sendEmptyMessage},
 * {@link #sendMessage}, {@link #sendMessageAtTime}, and
 * {@link #sendMessageDelayed} methods.  The <em>post</em> versions allow
 * you to enqueue Runnable objects to be called by the message queue when
 * they are received; the <em>sendMessage</em> versions allow you to enqueue
 * a {@link Message} object containing a bundle of data that will be
 * processed by the Handler's {@link #handleMessage} method (requiring that
 * you implement a subclass of Handler).
 * 
 * <p>When posting or sending to a Handler, you can either
 * allow the item to be processed as soon as the message queue is ready
 * to do so, or specify a delay before it gets processed or absolute time for
 * it to be processed.  The latter two allow you to implement timeouts,
 * ticks, and other timing-based behavior.
 * 
 * <p>When a
 * process is created for your application, its main thread is dedicated to
 * running a message queue that takes care of managing the top-level
 * application objects (activities, broadcast receivers, etc) and any windows
 * they create.  You can create your own threads, and communicate back with
 * the main application thread through a Handler.  This is done by calling
 * the same <em>post</em> or <em>sendMessage</em> methods as before, but from
 * your new thread.  The given Runnable or Message will then be scheduled
 * in the Handler's message queue and processed when appropriate.
 */
public class Handler {

真的是非常之长,也侧面证明了它的重要地位,和Java的Handle完全不是一个量啊。大意就是Handler 允许您发送和处理与线程的 MessageQueue 关联的 Message 和 Runnable 对象。这才对味嘛,我们安卓主要就是利用它去处理线程的呀。

然后官方又解释道,每个 Handler 实例都与单个线程和该线程的消息队列相关联。以及当您创建一个新的处理程序时,它会绑定到一个 Looper。它将消息和runnables 传递到该Looper 的消息队列并在该Looper 的线程上执行它们。就是有Looper的消息队列我们才能去让他去不断执行我们的需求

后面官方又另起一行说了Handle的主要功能:Handler 有两个主要用途:(1)安排消息和可运行文件在未来某个时间点执行; (2) 将要在与您自己的线程不同的线程上执行的操作排入队列。总结就是一可以延迟执行时间,二可以让子线程完成操作后通知主线程更新UI或其他线程触发后续操作。

在后面一大块都是对一些Handle的方法和属性的控制做一些介绍:调度消息是通过

post、postAtTime(Runnable, long)、postDelayed、sendEmptyMessage、sendMessage、sendMessageAtTime 和 sendMessageDelayed 方法完成的。

post 版本允许您将 Runnable 对象排入队列,以便在收到消息队列时调用它们; sendMessage 版本允许您将包含一组数据的 Message 对象排入队列,这些数据将由 Handler 的 handleMessage 方法处理(要求您实现 Handler 的子类)。

当请求或发送到Handler时,您可以允许在消息队列准备好后立即处理项目,或者指定处理之前的延迟一段时间或在一段时间操作的进程。后两者允许您实现超时、一定的周期数和其他基于时间的行为。

当为您的应用程序创建一个进程时,它的主线程专用于运行一个消息队列,该队列负责管理顶级应用程序对象(活动、广播接收器等)以及它们创建的任何窗口。您可以创建自己的线程,并通过 Handler 与主应用程序线程进行通信。

这是通过调用与以前相同的 post 或 sendMessage 方法来完成的,但来自您的新线程。然后,给定的 Runnable 或 Message 将被安排在 Handler 的消息队列中,并在适当的时候进行处理。

总结

今天终于发现错误,不然Java的源码都快看完了,虽然是表层的。今天主要看一些官方注释的翻译,贴出来,虽然机翻有点不好,但后续有时间会再优化一下。


相关文章
|
Java Maven Spring
如何使用Maven构建SpringBoot项目
如何使用Maven构建SpringBoot项目
|
Java
JSP 教程 之 JSP Session 3
该JSP教程介绍了如何使用HTTP Session处理无状态HTTP协议。示例展示了通过HttpSession对象获取会话的创建和最后访问时间,并利用session跟踪用户访问次数和ID。如果会话新建,则设置访问计数和用户ID;否则,更新访问计数。页面以表格形式展示会话ID、创建时间、最后访问时间、用户ID和访问次数。请尝试访问指定URL进行实践。
92 0
|
缓存 索引
OpenGL学习笔记(九):索引缓冲器(EBO /IBE)的理解与使用,引入线框/填充模式
OpenGL学习笔记(九):索引缓冲器(EBO /IBE)的理解与使用,引入线框/填充模式
OpenGL学习笔记(九):索引缓冲器(EBO /IBE)的理解与使用,引入线框/填充模式
|
前端开发
【休闲益智】【HTML】抓船长,30s抓船长,看能抓几只!
【休闲益智】【HTML】抓船长,30s抓船长,看能抓几只!
215 0
【休闲益智】【HTML】抓船长,30s抓船长,看能抓几只!
|
存储
CIO的慢IT:十大原则为你的IT减负
本文讲的是CIO的慢IT:十大原则为你的IT减负,多年来,IT专家们总是被告知他们必须少花钱、多做事。编制在缩减,工作在外包,与此同时对提升服务的要求和采用新技术的要求却在增加。过去一直是这样,但并不意味着将一直这样持续下去。
1668 0
|
14天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
6天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
9天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
856 31