让我们一起来看看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.
即:Handler是一个可以通过关联一个消息队列来发送或处理消息,发送或处理Runnable对象的一个处理程序,每个Handler都关联一个单个的线程和消息队列.当你创建一个新的Handler的时候它就将被绑定到一个线程或线程上的消息队列,从那时起,这个Handler就将为这个消息队列提供消息或Runnable对象,处理消息队列释放出来的消息或Runnable对象.
二.Handler有什么用?
-
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有两个主要的通途:
1.安排消息和Runnable对象在未来执行.
2.将你的一个动作放在不同的线程上执行.
三.Handler该怎么用?
-
Scheduling messages is accomplished with the
-
{@link #post}, {@link #postAtTime(Runnable, long)},
-
{@link #postDelayed}, {@link #sendEmptyMessage},
-
{@link #sendMessage}, {@link #sendMessageAtTime}, and
-
{@link #sendMessageDelayed} methods.
即:通过重写 * {@link #post}, {@link #postAtTime(Runnable, long)},
-
{@link #postDelayed}, {@link #sendEmptyMessage},
-
{@link #sendMessage}, {@link #sendMessageAtTime}, and
-
{@link #sendMessageDelayed} 来完成消息的发送
注:若有意的同志在查看Handler的源代码分析时,可了解以下几点:
Handler中的Message是分2种类型的,一种是DataMessage,也就是Message对象;另一种是CallbackMessage,就是Runnable对象,但是MessageQueue中只支持DataMessage,再插入到MessageQueue的时候,会把Runnable对象封装到一个Message对象中.
- 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).
即:当前版本允许你通过消息队列接受一个Runnable对象,sendMessage方法当前的版本允许你将一个包的数据通过消息队列的方式处理,但是你需要重写Handler的handleMessage方法.
- <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)尽快去处理已经准备好的条目,或者指定一个延迟处理的时间或者指定时间处理.后两个允许您实现超时,Ticks(系统相对的时间单位)和其他时间段为基础的行为.
- 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.
*/
即:当一个进程被应用程序创建时,它的主线程会运行一个消息队列负责管理它创建的高层应用程序对象(如activitie, broadcast receiver等)和任何它的窗口创建的对象.你可以通过一个Handler,创建自己的线程来实现与主线程之间的交互.但前提是你得在你的线程重写sendMessage方法并写上一行行代码你给定的Runnable或者Message将被MessageQueue(消息队列)预定,并在合适的时间~~~你懂得.
完整注释篇如下:
/**
- 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.
- <p>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.
- <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.
*/
最后附赠一张Handler的消息传递机制图解~:
末尾:移动互联&人力资源交流群,可加微信zy666128入群交流。
image.png
网友评论