前言
好家伙,写了四篇文章才发现自己看源码的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的源码都快看完了,虽然是表层的。今天主要看一些官方注释的翻译,贴出来,虽然机翻有点不好,但后续有时间会再优化一下。