Android消息机制三剑客之Handler

1、What is Handler?

    对与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.
 * 
 * <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.
 */

    Hanlder,你可以使用它发送或处理与当前线程的MessageQueue关联的Message或Runnable对象(这里使用Message时一般用于消息传递,使用Runnable时一般用于线程操作,后续会介绍)。每个Handler实例都会与一个单一的线程和该线程的消息队列关联。(面试题:那么这种关联是什么时候建立的呢?)每当你创建一个Handler实例时,它会与创建它的线程及该线程的消息队列进行绑定,在需要的时候,Handler就将message和runnable插入消息队列中,随着从队列中取出而执行。
    Handler主要由两种用途:(1)对message和runnable的执行进行调度;
(2)在其他线程中执行某个操作(B线程可以调用A线程的Handler从而在A线程中执行操作)。
    调度message有以下几种方法,功能根据字面含义就可知道,详细实现还请查看源码:
- post;
- postAtTime(Runnable,long);
- postDelayed;
- sendEmptyMessage;
- sendMessage;
- sendMessageAtTime;
- sendMessageDelayed;
    post系列的方法用于入队Runnable对象供message queue调用;sendMessage系列的方法用于入队Message对象,并且可以使用bundle携带信息用于传递,同时需要在自定义Handler子类中重写handleMessage方法,用于接受和处理收到的Message对象。而上述带AiTime或Delayed后缀的方法可以在指定时间或延后一定时间后再去执行(对应上述第(1)种用途)。
    当应用的进程被创建后,主线程(此处进程与线程的区别)中会维护一个消息队列用于管理高级别的应用对象(例如Activities、broadcast receivers 等等)和它们创建的一系列Window。我们可以通过Handler进行主线程和自创建线程之间的通信。也就是在子线程中调用主线程的Handler通过上述的post、send系列方法进行通信。发送的Message或Runnable对象会入队到主线程的消息队列,并在出队时执行(对应上述第(2)种用途)。

猜你喜欢

转载自blog.csdn.net/sdsxtianshi/article/details/80859646