---
title: "android源码解析--Handler"
author: "2129286"
date: "2014-05-10T11:12:54Z"
tags: []
categories: ["移动开发"]
slug: "android-handler"
draft: false
lastmod: "2014-05-10T11:12:54Z"
id: 2064574
url: "https://2129286.iteye.com/blog/2064574"
---

Handler是用于发送和处理消息和一个线程的MessageQueue相关联的Runable对象。每个Handler实例关联到一个单一线程和线程的messagequeue。当您创建一个Handler,从你创建它的时候开始，它就绑定到创建它的线程以及对应的消息队列，handler将发送消息到消息队列，并处理从消息队列中取出的消息。

Handler的主要用途有两个：(1)、在将来的某个时刻执行消息或一个runnable,(2)、为运行在不同线程中的多个任务排队。

   主要依靠post(Runnable)、postAtTime(Runnable, long)、postDelayed(Runnable, long)、sendEmptyMessage(int)、sendMessage(Message)、sendMessageAtTi(Message)、sendMessageDelayed(Message, long)这些方法来来完成消息调度。post方法是当到Runable对象到达就被插入到消息队列；sendMessage方法允许你把一个包含有信息的Message插入队列，而且它会Handler的handlerMessage(Message)方法中执行(该方法要求在Handler的子类中实现)。

当向Handler post或者send消息的时候，你可以在消息队列准备好的时候立刻执行，或者指定一个延迟之前得到处理或绝对时间对它进行处理，后两个是实现了timeout、ticks或者其他timing-based的行为。

当你的应用创建一个进程时，其主线程(UI线程)会运行一个消息队列,负责管理优先级最高的应用程序对象(活动、广播接收器等)和任何他们创建的windows。你也可以创建自己的线程，通过handler与主线程进行通信，通过在你创建的线程调用的post或sendMessage方法。传入的Runnable或者消息会被插入到消息队列并且在适当的时候得到处理。

 

先看下类里面使用的全局变量：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. final MessageQueue mQueue;
2. final Looper mLooper;
3. final Callback mCallback;
4. IMessenger mMessenger;

都会在构造方法里面赋值：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. /\**
02. * Default constructor associates this handler with the queue for the
03. * current thread.
04. \*
05. * If there isn't one, this handler won't be able to receive messages.
06. \*/
07. public Handler() {
08. if (FIND\_POTENTIAL\_LEAKS) {
09. final Class&lt;? extends Handler&gt; klass = getClass();
10. if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
11. (klass.getModifiers() & Modifier.STATIC) == 0) {
12. Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
13. klass.getCanonicalName());
14. }
15. }
16. mLooper = Looper.myLooper();
17. if (mLooper == null) {
18. throw new RuntimeException(
19. "Can't create handler inside thread that has not called Looper.prepare()");
20. }
21. mQueue = mLooper.mQueue;
22. mCallback = null;
23. }
24. /\**
25. * Constructor associates this handler with the queue for the
26. * current thread and takes a callback interface in which you can handle
27. * messages.
28. \*/
29. public Handler(Callback callback) {
30. if (FIND\_POTENTIAL\_LEAKS) {
31. final Class&lt;? extends Handler&gt; klass = getClass();
32. if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
33. (klass.getModifiers() & Modifier.STATIC) == 0) {
34. Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
35. klass.getCanonicalName());
36. }
37. }
38. mLooper = Looper.myLooper();
39. if (mLooper == null) {
40. throw new RuntimeException(
41. "Can't create handler inside thread that has not called Looper.prepare()");
42. }
43. mQueue = mLooper.mQueue;
44. mCallback = callback;
45. }
46. /\**
47. * Use the provided queue instead of the default one.
48. \*/
49. public Handler(Looper looper) {
50. mLooper = looper;
51. mQueue = looper.mQueue;
52. mCallback = null;
53. }
54. /\**
55. * Use the provided queue instead of the default one and take a callback
56. * interface in which to handle messages.
57. \*/
58. public Handler(Looper looper, Callback callback) {
59. mLooper = looper;
60. mQueue = looper.mQueue;
61. mCallback = callback;
62. }

 

在默认构造方法里面，handler是和当前线程的队列关联在一起，如果队列不存在，那么handler就不能接受消息。第二个有参构造方法中，需要传入一个callback接口用于处理handler传递的Message。第三个有参构造函数是传进来一个looper来代替默认的looper。第四个就是传递一个looper和callback。

 

在70行有个 FIND\_POTENTIAL\_LEAKS参数：找到潜在的泄露。看下注释：

 

**\[plain]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. /*
2. * Set this flag to true to detect anonymous, local or member classes
3. * that extend this Handler class and that are not static. These kind
4. * of classes can potentially create leaks.
5. \*/

 

 

设置这个标记为true来检测不是静态的匿名,本地或成员类继承Handler类。这些类型的类可以带来潜在的泄漏。在Handler的构造方法里面使用到了这个参数，目的就如上所述：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. if (FIND\_POTENTIAL\_LEAKS) {
2. final Class&lt;? extends Handler&gt; klass = getClass();
3. if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
4. (klass.getModifiers() & Modifier.STATIC) == 0) {
5. Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
6. klass.getCanonicalName());
7. }
8. }

 

 

接着下面就是Callback接口：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. /\**
2. * Callback interface you can use when instantiating a Handler to avoid
3. * having to implement your own subclass of Handler.
4. \*/
5. public interface Callback {
6. public boolean handleMessage(Message msg);
7. }

 

当你实例化一个Handler的时候可以使用Callback接口来避免写自定义的Handler子类。这里的机制类似与Thread与runable接口的关系。

在Handler里面，子类要处理消息的话必须重写handleMessage()这个方法，因为在handler里面它是个空方法：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. /\**
2. * Subclasses must implement this to receive messages.
3. \*/
4. public void handleMessage(Message msg) {
5. }

再来看一下：dispatchMessage()这个方法：

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. /\**
02. * Handle system messages here.
03. \*/
04. public void dispatchMessage(Message msg) {
05. if (msg.callback != null) {
06. handleCallback(msg);
07. } else {
08. if (mCallback != null) {
09. if (mCallback.handleMessage(msg)) {
10. return;
11. }
12. }
13. handleMessage(msg);
14. }
15. }

用于传递系统消息。当message的callback不为空的时候，调用handleCallback方法，如下：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. private final void handleCallback(Message message) {
2. message.callback.run();
3. }

 

关于调用Message的方法，在这篇文章里面先不谈，解析Message源码的时候再说。

下面是171行的getMessageName()方法：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;/\**
02. * Returns a string representing the name of the specified message.
03. * The default implementation will either return the class name of the
04. * message callback if any, or the hexadecimal representation of the
05. * message "what" field.
06. \*
07. * @param message The message whose name is being queried
08. \*/
09. public String getMessageName(Message message) {
10. if (message.callback != null) {
11. return message.callback.getClass().getName();
12. }
13. return "0x" + Integer.toHexString(message.what);
14. }&lt;/span&gt;

我们从源码中结合注释，返回传入message的name值，默认的实现是如火message.callback不为空，就返回callback的类名，或者返回一个16进制的message的what值。

 

再往下，191行的obtainMessage()方法：

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;  /\**
2. * Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
3. * creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
4. *  If you don't want that facility, just call Message.obtain() instead.
5. \*/
6. public final Message obtainMessage()
7. {
8. return Message.obtain(this);
9. }&lt;/span&gt;

从一个全局消息池里面获取一个新的Message。在Message池中检索是否存在与handler实例对应的message比创建一个新的Message更高效。如果你不想创建新Message，就是用Message.obtain方法代替。

 

下面是几个obtainMessage的重载方法：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;    /\**
02. \*
03. * Same as {@link #obtainMessage()}, except that it also sets the what and obj members
04. * of the returned Message.
05. \*
06. * @param what Value to assign to the returned Message.what field.
07. * @param obj Value to assign to the returned Message.obj field.
08. * @return A Message from the global message pool.
09. \*/
10. public final Message obtainMessage(int what, Object obj)
11. {
12. return Message.obtain(this, what, obj);
13. }
14. /\**
15. \*
16. * Same as {@link #obtainMessage()}, except that it also sets the what, arg1 and arg2 members of the returned
17. * Message.
18. * @param what Value to assign to the returned Message.what field.
19. * @param arg1 Value to assign to the returned Message.arg1 field.
20. * @param arg2 Value to assign to the returned Message.arg2 field.
21. * @return A Message from the global message pool.
22. \*/
23. public final Message obtainMessage(int what, int arg1, int arg2)
24. {
25. return Message.obtain(this, what, arg1, arg2);
26. }
27. /\**
28. \*
29. * Same as {@link #obtainMessage()}, except that it also sets the what, obj, arg1,and arg2 values on the
30. * returned Message.
31. * @param what Value to assign to the returned Message.what field.
32. * @param arg1 Value to assign to the returned Message.arg1 field.
33. * @param arg2 Value to assign to the returned Message.arg2 field.
34. * @param obj Value to assign to the returned Message.obj field.
35. * @return A Message from the global message pool.
36. \*/
37. public final Message obtainMessage(int what, int arg1, int arg2, Object obj)
38. {
39. return Message.obtain(this, what, arg1, arg2, obj);
40. }&lt;/span&gt;

和上面相同，只是参数不同，为返回的Message的一些属性赋值。

 

在往下就是post（）方法了：

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;/\**
02. * Causes the Runnable r to be added to the message queue.
03. * The runnable will be run on the thread to which this handler is
04. * attached.
05. \*
06. * @param r The Runnable that will be executed.
07. \*
08. * @return Returns true if the Runnable was successfully placed in to the
09. *         message queue.  Returns false on failure, usually because the
10. *         looper processing the message queue is exiting.
11. \*/
12. public final boolean post(Runnable r)
13. {
14. return  sendMessageDelayed(getPostMessage(r), 0);
15. }&lt;/span&gt;

把传入的Runnable对象r加入到Message队列中，这个runnable对象将在handler关联的线程中执行。如果runnable对象被正确执行返回true，如果looper遍历消息队列时退出，则返回false。在这个方法中，主要是调用了sendMessageDelayed方法。在下面会有相应的分析。

 

接下来，看一下其他有关post的方法（从266行到353行）：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;   /\**
02. * Causes the Runnable r to be added to the message queue, to be run
03. * at a specific time given by &lt;var&gt;uptimeMillis&lt;/var&gt;.
04. * &lt;b&gt;The time-base is {@link android.os.SystemClock#uptimeMillis}.&lt;/b&gt;
05. * The runnable will be run on the thread to which this handler is attached.
06. \*
07. * @param r The Runnable that will be executed.
08. * @param uptimeMillis The absolute time at which the callback should run,
09. *         using the {@link android.os.SystemClock#uptimeMillis} time-base.
10. \*
11. * @return Returns true if the Runnable was successfully placed in to the
12. *         message queue.  Returns false on failure, usually because the
13. *         looper processing the message queue is exiting.  Note that a
14. *         result of true does not mean the Runnable will be processed -- if
15. *         the looper is quit before the delivery time of the message
16. *         occurs then the message will be dropped.
17. \*/
18. public final boolean postAtTime(Runnable r, long uptimeMillis)
19. {
20. return sendMessageAtTime(getPostMessage(r), uptimeMillis);
21. }
22. /\**
23. * Causes the Runnable r to be added to the message queue, to be run
24. * at a specific time given by &lt;var&gt;uptimeMillis&lt;/var&gt;.
25. * &lt;b&gt;The time-base is {@link android.os.SystemClock#uptimeMillis}.&lt;/b&gt;
26. * The runnable will be run on the thread to which this handler is attached.
27. \*
28. * @param r The Runnable that will be executed.
29. * @param uptimeMillis The absolute time at which the callback should run,
30. *         using the {@link android.os.SystemClock#uptimeMillis} time-base.
31. \*
32. * @return Returns true if the Runnable was successfully placed in to the
33. *         message queue.  Returns false on failure, usually because the
34. *         looper processing the message queue is exiting.  Note that a
35. *         result of true does not mean the Runnable will be processed -- if
36. *         the looper is quit before the delivery time of the message
37. *         occurs then the message will be dropped.
38. \*
39. * @see android.os.SystemClock#uptimeMillis
40. \*/
41. public final boolean postAtTime(Runnable r, Object token, long uptimeMillis)
42. {
43. return sendMessageAtTime(getPostMessage(r, token), uptimeMillis);
44. }
45. /\**
46. * Causes the Runnable r to be added to the message queue, to be run
47. * after the specified amount of time elapses.
48. * The runnable will be run on the thread to which this handler
49. * is attached.
50. \*
51. * @param r The Runnable that will be executed.
52. * @param delayMillis The delay (in milliseconds) until the Runnable
53. *        will be executed.
54. \*
55. * @return Returns true if the Runnable was successfully placed in to the
56. *         message queue.  Returns false on failure, usually because the
57. *         looper processing the message queue is exiting.  Note that a
58. *         result of true does not mean the Runnable will be processed --
59. *         if the looper is quit before the delivery time of the message
60. *         occurs then the message will be dropped.
61. \*/
62. public final boolean postDelayed(Runnable r, long delayMillis)
63. {
64. return sendMessageDelayed(getPostMessage(r), delayMillis);
65. }
66. /\**
67. * Posts a message to an object that implements Runnable.
68. * Causes the Runnable r to executed on the next iteration through the
69. * message queue. The runnable will be run on the thread to which this
70. * handler is attached.
71. * &lt;b&gt;This method is only for use in very special circumstances -- it
72. * can easily starve the message queue, cause ordering problems, or have
73. * other unexpected side-effects.&lt;/b&gt;
74. \*
75. * @param r The Runnable that will be executed.
76. \*
77. * @return Returns true if the message was successfully placed in to the
78. *         message queue.  Returns false on failure, usually because the
79. *         looper processing the message queue is exiting.
80. \*/
81. public final boolean postAtFrontOfQueue(Runnable r)
82. {
83. return sendMessageAtFrontOfQueue(getPostMessage(r));
84. }
85. &lt;/span&gt;

postAtTime在指定时间uptimeMillis把runnable插入到队列中去，另一个postAtTime方法又加了一个Object类型的token，在下面的sendMessageAtTime中具体分析。postDelayed在延迟delayMillis时间后插入队列。postAtFrontOfQueue把Runnable插入到队首，下一次轮询就会被执行。

 

下面是从队列中删除对应的runable：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;    /\**
02. * Remove any pending posts of Runnable r that are in the message queue.
03. \*/
04. public final void removeCallbacks(Runnable r)
05. {
06. mQueue.removeMessages(this, r, null);
07. }
08. /\**
09. * Remove any pending posts of Runnable &lt;var&gt;r&lt;/var&gt; with Object
10. * &lt;var&gt;token&lt;/var&gt; that are in the message queue.  If &lt;var&gt;token&lt;/var&gt; is null,
11. * all callbacks will be removed.
12. \*/
13. public final void removeCallbacks(Runnable r, Object token)
14. {
15. mQueue.removeMessages(this, r, token);
16. }
17. &lt;/span&gt;

下面就是重头戏SendMessage：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt; /\**
02. * Pushes a message onto the end of the message queue after all pending messages
03. * before the current time. It will be received in {@link #handleMessage},
04. * in the thread attached to this handler.
05. \*
06. * @return Returns true if the message was successfully placed in to the
07. *         message queue.  Returns false on failure, usually because the
08. *         looper processing the message queue is exiting.
09. \*/
10. public final boolean sendMessage(Message msg)
11. {
12. return sendMessageDelayed(msg, 0);
13. }&lt;/span&gt;

把一个消息插入到当前所有正在等待执行的消息的后面。它会在当前线程所关联的handler的handleMessage方法中被处理。我们看到这个方法主要是调用了441行的sendMessageDelayed方法（延迟0秒发送消息）：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;/\**
02. * Enqueue a message into the message queue after all pending messages
03. * before (current time + delayMillis). You will receive it in
04. * {@link #handleMessage}, in the thread attached to this handler.
05. \*
06. * @return Returns true if the message was successfully placed in to the
07. *         message queue.  Returns false on failure, usually because the
08. *         looper processing the message queue is exiting.  Note that a
09. *         result of true does not mean the message will be processed -- if
10. *         the looper is quit before the delivery time of the message
11. *         occurs then the message will be dropped.
12. \*/
13. public final boolean sendMessageDelayed(Message msg, long delayMillis)
14. {
15. if (delayMillis &lt; 0) {
16. delayMillis = 0;
17. }
18. return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
19. }&lt;/span&gt;

这个方法主要是在delayMillis时间后发送消息。调用的是467行的sendMessageAtTime方法：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;/\**
02. * Enqueue a message into the message queue after all pending messages
03. * before the absolute time (in milliseconds) &lt;var&gt;uptimeMillis&lt;/var&gt;.
04. * &lt;b&gt;The time-base is {@link android.os.SystemClock#uptimeMillis}.&lt;/b&gt;
05. * You will receive it in {@link #handleMessage}, in the thread attached
06. * to this handler.
07. \*
08. * @param uptimeMillis The absolute time at which the message should be
09. *         delivered, using the
10. *         {@link android.os.SystemClock#uptimeMillis} time-base.
11. \*
12. * @return Returns true if the message was successfully placed in to the
13. *         message queue.  Returns false on failure, usually because the
14. *         looper processing the message queue is exiting.  Note that a
15. *         result of true does not mean the message will be processed -- if
16. *         the looper is quit before the delivery time of the message
17. *         occurs then the message will be dropped.
18. \*/
19. public boolean sendMessageAtTime(Message msg, long uptimeMillis)
20. {
21. boolean sent = false;
22. MessageQueue queue = mQueue;
23. if (queue != null) {
24. msg.target = this;
25. sent = queue.enqueueMessage(msg, uptimeMillis);
26. }
27. else {
28. RuntimeException e = new RuntimeException(
29. this + " sendMessageAtTime() called with no mQueue");
30. Log.w("Looper", e.getMessage(), e);
31. }
32. return sent;
33. }&lt;/span&gt;

这个方法才是真正执行插入到队列的操作，把message插入到消息队列中。

 

像386行到427行等发送消息，均是调用sendMessageAtTime方法：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;    /\**
02. * Sends a Message containing only the what value.
03. \*
04. * @return Returns true if the message was successfully placed in to the
05. *         message queue.  Returns false on failure, usually because the
06. *         looper processing the message queue is exiting.
07. \*/
08. public final boolean sendEmptyMessage(int what)
09. {
10. return sendEmptyMessageDelayed(what, 0);
11. }
12. /\**
13. * Sends a Message containing only the what value, to be delivered
14. * after the specified amount of time elapses.
15. * @see #sendMessageDelayed(android.os.Message, long)
16. \*
17. * @return Returns true if the message was successfully placed in to the
18. *         message queue.  Returns false on failure, usually because the
19. *         looper processing the message queue is exiting.
20. \*/
21. public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
22. Message msg = Message.obtain();
23. msg.what = what;
24. return sendMessageDelayed(msg, delayMillis);
25. }
26. /\**
27. * Sends a Message containing only the what value, to be delivered
28. * at a specific time.
29. * @see #sendMessageAtTime(android.os.Message, long)
30. \*
31. * @return Returns true if the message was successfully placed in to the
32. *         message queue.  Returns false on failure, usually because the
33. *         looper processing the message queue is exiting.
34. \*/
35. public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
36. Message msg = Message.obtain();
37. msg.what = what;
38. return sendMessageAtTime(msg, uptimeMillis);
39. }&lt;/span&gt;

发送空消息，使用Message.obtain()方法获得一个Message（上面已经讲道这个方法）进行发送。

 

在往下495行sendMessageAtFrotOfQueue：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt; /\**
02. * Enqueue a message at the front of the message queue, to be processed on
03. * the next iteration of the message loop.  You will receive it in
04. * {@link #handleMessage}, in the thread attached to this handler.
05. * &lt;b&gt;This method is only for use in very special circumstances -- it
06. * can easily starve the message queue, cause ordering problems, or have
07. * other unexpected side-effects.&lt;/b&gt;
08. \*
09. * @return Returns true if the message was successfully placed in to the
10. *         message queue.  Returns false on failure, usually because the
11. *         looper processing the message queue is exiting.
12. \*/
13. public final boolean sendMessageAtFrontOfQueue(Message msg)
14. {
15. boolean sent = false;
16. MessageQueue queue = mQueue;
17. if (queue != null) {
18. msg.target = this;
19. sent = queue.enqueueMessage(msg, 0);
20. }
21. else {
22. RuntimeException e = new RuntimeException(
23. this + " sendMessageAtTime() called with no mQueue");
24. Log.w("Looper", e.getMessage(), e);
25. }
26. return sent;
27. }&lt;/span&gt;

把一个消息插入到message queue的队首。但是我们注意到在SendMessageAtTime中插入队列代码：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;queue.enqueueMessage(msg, uptimeMillis);&lt;/span&gt;

在uptimeMillis时间后插入到队列，而在sendMessageAtFrotOfQueue中插入队列代码：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;queue.enqueueMessage(msg, 0)&lt;/span&gt;

按照字面意思理解，就是立即插入队列，但是立刻插入队列也不能实现插到队首。那到底是如何实现的哪？这一点，将在[MessageQueue源码分析](http://blog.csdn.net/aomandeshangxiao/article/details/8143663)中揭晓。

 

从511行到535行是从消息队列中删除对应的消息：

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;    /\**
02. * Remove any pending posts of messages with code 'what' that are in the
03. * message queue.
04. \*/
05. public final void removeMessages(int what) {
06. mQueue.removeMessages(this, what, null, true);
07. }
08. /\**
09. * Remove any pending posts of messages with code 'what' and whose obj is
10. * 'object' that are in the message queue.  If &lt;var&gt;token&lt;/var&gt; is null,
11. * all messages will be removed.
12. \*/
13. public final void removeMessages(int what, Object object) {
14. mQueue.removeMessages(this, what, object, true);
15. }
16. /\**
17. * Remove any pending posts of callbacks and sent messages whose
18. * &lt;var&gt;obj&lt;/var&gt; is &lt;var&gt;token&lt;/var&gt;.  If &lt;var&gt;token&lt;/var&gt; is null,
19. * all callbacks and messages will be removed.
20. \*/
21. public final void removeCallbacksAndMessages(Object token) {
22. mQueue.removeCallbacksAndMessages(this, token);
23. }&lt;/span&gt;

541行，检查消息队列中是否存在相对应的消息：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt;   /\**
02. * Check if there are any pending posts of messages with code 'what' in
03. * the message queue.
04. \*/
05. public final boolean hasMessages(int what) {
06. return mQueue.removeMessages(this, what, null, false);
07. }
08. /\**
09. * Check if there are any pending posts of messages with code 'what' and
10. * whose obj is 'object' in the message queue.
11. \*/
12. public final boolean hasMessages(int what, Object object) {
13. return mQueue.removeMessages(this, what, object, false);
14. }&lt;/span&gt;

555行：获取当前looper：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;public final Looper getLooper() {
2. return mLooper;
3. }&lt;/span&gt;

往下，dump方法，从字面意思上理解：转储，具体作用，还不太了解，将在[Looper源码解析](http://blog.csdn.net/aomandeshangxiao/article/details/8145320)中分析下。

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt; public final void dump(Printer pw, String prefix) {
2. pw.println(prefix + this + " @ " + SystemClock.uptimeMillis());
3. if (mLooper == null) {
4. pw.println(prefix + "looper uninitialized");
5. } else {
6. mLooper.dump(pw, prefix + "  ");
7. }
8. }&lt;/span&gt;

575行，获取当前Messenger：

 

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;final IMessenger getIMessenger() {
2. synchronized (mQueue) {
3. if (mMessenger != null) {
4. return mMessenger;
5. }
6. mMessenger = new MessengerImpl();
7. return mMessenger;
8. }
9. }&lt;/span&gt;

关于Messenger信使类，请关注以后源码分析。

 

591行：将一个Runnable封装成一个Message。

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

01. &lt;span style="font-size:14px;"&gt; private final Message getPostMessage(Runnable r) {
02. Message m = Message.obtain();
03. m.callback = r;
04. return m;
05. }
06. private final Message getPostMessage(Runnable r, Object token) {
07. Message m = Message.obtain();
08. m.obj = token;
09. m.callback = r;
10. return m;
11. }&lt;/span&gt;

getPostMessage这个方法在上面所说的post系列方法中，被广泛使用。

 

最后，604行，处理message里面的runnable消息，直接调用了run方法。

 

**\[java]** [view plain](http://blog.csdn.net/lilu_leo/article/details/8143205 "view plain")[copy](http://blog.csdn.net/lilu_leo/article/details/8143205 "copy")

1. &lt;span style="font-size:14px;"&gt;private final void handleCallback(Message message) {
2. message.callback.run();
3. }&lt;/span&gt;