Android消息机制

handler的运行离不开looper和messagequeen的支持,我们使用looper.prepare(looper.prepare(boolean quitAllowed))来创建一个looper,prepare方法中将创建的loopear存放到threadlocal(在指定的线程中存储数据,数据存储后,只能在指定的线程中读取)中,

image.png

在loopear的构造方法中,会创建一个messageQueen,用来存放message,至此,looper的准备工作已经完成

image.png

当我们调用looper.loop()方法时,会判断loopear有没有初始化,没有则抛出异常,拿到初始化的looper后,通过looper获取lopper中创建的messageQueen,然后通过死循环,不停的通过messageQueen.next()获取消息,当没有消息时将会阻塞等待新消息入队,直到messageQueen.next()返回null时,结束循环。当looper.loop方法中接收到消息时,会调用msg.target.dispatchMessage(msg);也就是handler的dispatchMessage(msg)方法,将消息交由handler处理,在Handler中会通过looper.myLooper()获取looper,在handler调用sendMessage(Message msg)方法时,会将message通过queue.enqueueMessage(msg, uptimeMillis);放入MessageQueen中,唤醒looper.loop的阻塞等待,进行后续处理。

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
    MessageQueue queue = mQueue;
    if (queue == null) {
        RuntimeException e = new RuntimeException(
                this + " sendMessageAtTime() called with no mQueue");
        Log.w("Looper", e.getMessage(), e);
        return false;
    }
   // 将message放入messageQueen `方法内部调用queue.enqueueMessage(msg, uptimeMillis);`
    return enqueueMessage(queue, msg, uptimeMillis);
}
  • looper.loop()

      public static void loop() {
      //判断looper有没有初始化
      final Looper me = myLooper();
      if (me == null) {
          throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this         
      thread.");
      }
      final MessageQueue queue = me.mQueue;
    
      // 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();
    
      // Allow overriding a threshold with a system prop. e.g.
      // adb shell 'setprop log.looper.1000.main.slow 1 && stop && start'
      final int thresholdOverride =
              SystemProperties.getInt("log.looper."
                      + Process.myUid() + "."
                      + Thread.currentThread().getName()
                      + ".slow", 0);
    
      boolean slowDeliveryDetected = false;
    
      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
          final Printer logging = me.mLogging;
          if (logging != null) {
              logging.println(">>>>> Dispatching to " + msg.target + " " +
                      msg.callback + ": " + msg.what);
          }
    
          final long traceTag = me.mTraceTag;
          long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
          long slowDeliveryThresholdMs = me.mSlowDeliveryThresholdMs;
          if (thresholdOverride > 0) {
              slowDispatchThresholdMs = thresholdOverride;
              slowDeliveryThresholdMs = thresholdOverride;
          }
          final boolean logSlowDelivery = (slowDeliveryThresholdMs > 0) && (msg.when > 0);
          final boolean logSlowDispatch = (slowDispatchThresholdMs > 0);
    
          final boolean needStartTime = logSlowDelivery || logSlowDispatch;
          final boolean needEndTime = logSlowDispatch;
    
          if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
              Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
          }
    
          final long dispatchStart = needStartTime ? SystemClock.uptimeMillis() : 0;
          final long dispatchEnd;
          try {
              msg.target.dispatchMessage(msg);
              dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
          } finally {
              if (traceTag != 0) {
                  Trace.traceEnd(traceTag);
              }
          }
          if (logSlowDelivery) {
              if (slowDeliveryDetected) {
                  if ((dispatchStart - msg.when) <= 10) {
                      Slog.w(TAG, "Drained");
                      slowDeliveryDetected = false;
                  }
              } else {
                  if (showSlowLog(slowDeliveryThresholdMs, msg.when, dispatchStart, "delivery",
                          msg)) {
                      // Once we write a slow delivery log, suppress until the queue drains.
                      slowDeliveryDetected = true;
                  }
              }
          }
          if (logSlowDispatch) {
              showSlowLog(slowDispatchThresholdMs, dispatchStart, dispatchEnd, "dispatch", msg);
          }
    
          if (logging != null) {
              logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
          }
    
          // Make sure that during the course of dispatching the
          // identity of the thread wasn't corrupted.
          final long newIdent = Binder.clearCallingIdentity();
          if (ident != newIdent) {
              Log.wtf(TAG, "Thread identity changed from 0x"
                      + Long.toHexString(ident) + " to 0x"
                      + Long.toHexString(newIdent) + " while dispatching to "
                      + msg.target.getClass().getName() + " "
                      + msg.callback + " what=" + msg.what);
          }
    
          msg.recycleUnchecked();
      }
    

    }

你可能感兴趣的:(Android消息机制)