Android handle-message的发送与处理案例详解

  /**

  * Run the message queue in this thread. Be sure to call

  * {@link #quit()} to end the loop.

  */

  public static void loop() {

  final Looper me = myLooper();//返回保存在调用线程TLV中的Looper对象

  if (me == null) {

  throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

  }

  final MessageQueue queue = me.mQueue;//取得Looper对象的消息队列

  // Make sure the identity of this thread is that of the local process,

  // and keep track of what that identity token actually is.

  Binder.clearCallingIdentity();

  final long ident = Binder.clearCallingIdentity();

  for (;;) {

  Message msg = queue.next(); // might block 取消息队列中的一个待处理消息

  if (msg == null) {

  // No message indicates that the message queue is quitting.

  return;

  }

  // This must be in a local variable, in case a UI event sets the logger

  Printer logging = me.mLogging;

  if (logging != null) {

  logging.println(">>>>> Dispatching to " + msg.target + " " +

  msg.callback + ": " + msg.what);

  }

  msg.target.dispatchMessage(msg);//调用该消息的Handle,交给它的dispatchMessage函数处理

  }

  }