Run the message queue in this thread. Be sure to call #quit() to end the loop.
()
| 108 | * {@link #quit()} to end the loop. |
| 109 | */ |
| 110 | public static void loop() { |
| 111 | final Looper me = myLooper(); |
| 112 | if (me == null) { |
| 113 | throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread."); |
| 114 | } |
| 115 | final MessageQueue queue = me.mQueue; |
| 116 | |
| 117 | // Make sure the identity of this thread is that of the local process, |
| 118 | // and keep track of what that identity token actually is. |
| 119 | Binder.clearCallingIdentity(); |
| 120 | final long ident = Binder.clearCallingIdentity(); |
| 121 | |
| 122 | for (;;) { |
| 123 | Message msg = queue.next(); // might block |
| 124 | if (msg == null) { |
| 125 | // No message indicates that the message queue is quitting. |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | // This must be in a local variable, in case a UI event sets the logger |
| 130 | Printer logging = me.mLogging; |
| 131 | if (logging != null) { |
| 132 | logging.println(">>>>> Dispatching to " + msg.target + " " + |
| 133 | msg.callback + ": " + msg.what); |
| 134 | } |
| 135 | |
| 136 | msg.target.dispatchMessage(msg); |
| 137 | |
| 138 | if (logging != null) { |
| 139 | logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); |
| 140 | } |
| 141 | |
| 142 | // Make sure that during the course of dispatching the |
| 143 | // identity of the thread wasn't corrupted. |
| 144 | final long newIdent = Binder.clearCallingIdentity(); |
| 145 | if (ident != newIdent) { |
| 146 | Log.wtf(TAG, "Thread identity changed from 0x" |
| 147 | + Long.toHexString(ident) + " to 0x" |
| 148 | + Long.toHexString(newIdent) + " while dispatching to " |
| 149 | + msg.target.getClass().getName() + " " |
| 150 | + msg.callback + " what=" + msg.what); |
| 151 | } |
| 152 | |
| 153 | msg.recycle(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Return the Looper object associated with the current thread. Returns |