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 the thread /
* message queue of the thread that is creating it -- from that point on,
* it will deliver messages and runnables to that message queue and execute
* them as they come out of the message queue.
这句话介绍了什么是 Hanlder 大概意思是说(这种格式看起来比较容易):
* 一个 Hanlder 允许你去发送和处理与 Thread(线程) 的 MessageQueue(消息队列) 相关联的
* Message(消息) 和 Runnable 对象。每个 Hanlder 实例关联一个 Thread 实例和该 Thread 的
* MessageQueue。当你创建一个新的 Hanlder 时,它(Handler)会和当前 Thread/当前 Thread 的
* MessageQueue 联系起来 -- 此时,它(Handler)会将 Messages 和 Runnables 传递到该 MessageQueue
* 并在他们从消息队列中出现时执行它们。
- 第二段话:
* There are two main uses for a Handler: (1) to schedule messages and
* runnables to be executed as some point in the future; and (2) to enqueue
* an action to be performed on a different thread than your own.
这句话介绍了 Handler 的两个主要用途:
- 传入 Message 和 Runnable 用来执行。
- 在另一个线程上执行一个操作。
- 第三段话:
* cheduling 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).
这句话首先介绍了 Handler 调度 Message 的几种方法:
- post
- postAtTime(Runnable,long)
- postDelayed
- sendEmptyMessage
- sendMessage
- sendMessageAtTime
- sendMessageDelayed
post 允许你在接收到 MessageQueue 时调用 Runnable 对象。
sendMessage 允许你对包含一些数据的 Message 对象进行排列,这些数据将由 handlerMessage 方法处理。
- 第四段话:
* <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.
大概意思是:
当发布或者发送到 Handler 时,只要 MessageQueue 准备就绪就可以立即处理该任务。或者在处理 MessageQueue 之前指定一个延迟或者一个固定的时间。后两者允许你实现延时和一些其他的时间设置。
- 最后一段:
* 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.
当一个进程在应用中被创建之后,它的主线程致力于运行一个负责管理顶层应用对象(Activity,Broadcast Receiver,etc)以及它创建一些 Window 的 MesssageQueue。你可以创建一些你自己的线程(子线程)来通过 Handler 和主线程通信。和以前一样,它需要调用 post 和 sendMessage 方法来完成,但是要在你新创建的子线程中。传递过来的 Message 或者 Runnable 将会在 Handler 的 MessageQueue 中调度,并在合适的时候处理。