美文网首页
Handler和Looper

Handler和Looper

作者: tjy_2011 | 来源:发表于2018-10-10 22:20 被阅读0次

源码路径可以参考:http://androidxref.com/9.0.0_r3/

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

简单概括下就是,Handler有两个用处:可以暂存消息(Message)或者Runnable对象,使得它们可以在未来(基于时间)的时候被处理或者执行;可以把消息(Message)或者Runnable对象放到其他的线程(Thread)上面去执行。

每个 Handler实例和一个线程以及线程上的消息队列(MessageQueue)相关联. 当创建一个新的Handler的时候,它就和创建它的线程以及消息队列绑在一起,并向线程上的消息队列发送消息或者Runnable对象,在消息或者Runnable对象出消息队列的时候被处理或者被运行。

Handler自己无法完成这个功能,必须和Looper一起来完成。

在Handler的几个构造函数中,Looper可以通过参数传递,如果没有通过参数传递,则使用Looper.myLooper()调用返回的Looper。

Looper很特别,它是绑定在thread上面,并且read上面只有一个Looper实例,在这个thread上面发起loop循环。

回到上面Handler的Looper,如果这个Looper是其他thread上面创建的,则Looper在其他的thread上面发起loop循环;如果没有指定这个Looper实例,则在当前thread上面发起loop循环。

Looper有一个MessageQueue mQueue,也会传递给Handler。
Handler把Message的target设置成当前这个Handler, 然后把Message放到这个Messageueue上面。Looper的loop循环会收到这个Message,并调用Message的callback或者调用Handler子类定义的handleMessage(注意,这个callback或者handleMessage是在创建Looper的thread上面运行的,如果这个thread不是UI thread,则在 callback或者handleMessage中不能访问UI元素)。

Handler在发送Message的时候可以设置delay时间,在处理的时候是转换成绝对时间,传递到C代码中,使用epoll_wait,设置其timeout时间来实现的。

从上面可以发现,Looper是一个背后英雄。

相关文章

网友评论

      本文标题:Handler和Looper

      本文链接:https://www.haomeiwen.com/subject/yczmaftx.html