Android Handler类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chennai1101/article/details/83958256

相关文章
Android AsyncTask类
Android Handler类
Android IntentService应用

1. Handler类

Handler类被用于异步消息处理线程,常被用来更新UI线程。

  • 一般来说我们会在主线程中创建一个Handler的匿名内部类,然后重写它的handleMessage方法来处理我们的UI操作。

      private final static int UPDATE_TIME = 1;
    
      private TextView mTv;
      private Handler mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              switch (msg.what) {
                  // 根据what的值处理不同操作
                  case UPDATE_TIME:
                      mTv.setText(getCurrentTime());
                      break;
              }
          }
      };
    

    调用Handler的sendMessage方法来发送消息。

      mHandler.sendEmptyMessage(UPDATE_TIME);
    

    其他类似方法

      sendEmptyMessage(int what)
      sendEmptyMessageAtTime(int what, long uptimeMillis)
      sendEmptyMessageDelayed(int what, long delayMillis)
      sendMessage(Message msg)
      sendMessageAtFrontOfQueue(Message msg)
      sendMessageAtTime(Message msg, long uptimeMillis)
      sendMessageDelayed(Message msg, long delayMillis)
    
  • 在线程中调用post方法

      mHandler.post(new Runnable() {
          @Override
          public void run() {
              mTv.setText(getCurrentTime());
          }
      });
    

    其他类似方法

      post(Runnable r)
      postAtFrontOfQueue(Runnable r)
      postAtTime(Runnable r, long uptimeMillis)
      postAtTime(Runnable r, Object token, long uptimeMillis)
      postDelayed(Runnable r, long delayMillis)
    

2. Handler类解析

Android的消息机制中主要是由Handler,Looper,MessageQueue,Message等组成。

  • Looper主要是prepare()和loop()两个方法。

    首先是prepare方法,sThreadLocal是一个ThreadLocal对象,可以在一个线程中存储变量。prepare方法不能被调用两次,这保证了一个线程中只有一个Looper实例。

      public static void prepare() {
          prepare(true);
      }
    
      private static void prepare(boolean quitAllowed) {
          if (sThreadLocal.get() != null) {
              throw new RuntimeException("Only one Looper may be created per thread");
          }
          sThreadLocal.set(new Looper(quitAllowed));
      }
    

    Looper的构造函数,创建一个消息队列。

    扫描二维码关注公众号,回复: 4030210 查看本文章
      private Looper(boolean quitAllowed) {
          mQueue = new MessageQueue(quitAllowed);
          mThread = Thread.currentThread();
      }
    

    loop方法,先获得当前线程的Looper,调用消息队列的next方法获取消息,随后回调Handler的dispatchMessage来处理消息。

      public static void loop() {
          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();
    
          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 slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
    
              final long traceTag = me.mTraceTag;
              if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                  Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
              }
              final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
              final long end;
              try {
                  msg.target.dispatchMessage(msg);
                  end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
              } finally {
                  if (traceTag != 0) {
                      Trace.traceEnd(traceTag);
                  }
              }
              if (slowDispatchThresholdMs > 0) {
                  final long time = end - start;
                  if (time > slowDispatchThresholdMs) {
                      Slog.w(TAG, "Dispatch took " + time + "ms on "
                              + Thread.currentThread().getName() + ", h=" +
                              msg.target + " cb=" + msg.callback + " msg=" + msg.what);
                  }
              }
    
              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();
          }
      }
    
      public static @Nullable Looper myLooper() {
          return sThreadLocal.get();
      }
    
  • MessageQueue是消息队列

    enqueueMessage中加入消息。如果当前没有消息,或者when小于队列头的话,作为新的队列头。否则插入到队伍中。

      boolean enqueueMessage(Message msg, long when) {
          if (msg.target == null) {
              throw new IllegalArgumentException("Message must have a target.");
          }
          if (msg.isInUse()) {
              throw new IllegalStateException(msg + " This message is already in use.");
          }
    
          synchronized (this) {
              if (mQuitting) {
                  IllegalStateException e = new IllegalStateException(
                          msg.target + " sending message to a Handler on a dead thread");
                  Log.w(TAG, e.getMessage(), e);
                  msg.recycle();
                  return false;
              }
    
              msg.markInUse();
              msg.when = when;
              Message p = mMessages;
              boolean needWake;
              if (p == null || when == 0 || when < p.when) {
                  // New head, wake up the event queue if blocked.
                  msg.next = p;
                  mMessages = msg;
                  needWake = mBlocked;
              } else {
                  // Inserted within the middle of the queue.  Usually we don't have to wake
                  // up the event queue unless there is a barrier at the head of the queue
                  // and the message is the earliest asynchronous message in the queue.
                  needWake = mBlocked && p.target == null && msg.isAsynchronous();
                  Message prev;
                  for (;;) {
                      prev = p;
                      p = p.next;
                      if (p == null || when < p.when) {
                          break;
                      }
                      if (needWake && p.isAsynchronous()) {
                          needWake = false;
                      }
                  }
                  msg.next = p; // invariant: p == prev.next
                  prev.next = msg;
              }
    
              // We can assume mPtr != 0 because mQuitting is false.
              if (needWake) {
                  nativeWake(mPtr);
              }
          }
          return true;
      }
    

    next方法中获取消息。

      Message next() {
          // Return here if the message loop has already quit and been disposed.
          // This can happen if the application tries to restart a looper after quit
          // which is not supported.
          final long ptr = mPtr;
          if (ptr == 0) {
              return null;
          }
    
          int pendingIdleHandlerCount = -1; // -1 only during first iteration
          int nextPollTimeoutMillis = 0;
          for (;;) {
              if (nextPollTimeoutMillis != 0) {
                  Binder.flushPendingCommands();
              }
    
              nativePollOnce(ptr, nextPollTimeoutMillis);
    
              synchronized (this) {
                  // Try to retrieve the next message.  Return if found.
                  final long now = SystemClock.uptimeMillis();
                  Message prevMsg = null;
                  Message msg = mMessages;
                  if (msg != null && msg.target == null) {
                      // Stalled by a barrier.  Find the next asynchronous message in the queue.
                      do {
                          prevMsg = msg;
                          msg = msg.next;
                      } while (msg != null && !msg.isAsynchronous());
                  }
                  if (msg != null) {
                      if (now < msg.when) {
                          // Next message is not ready.  Set a timeout to wake up when it is ready.
                          nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                      } else {
                          // Got a message.
                          mBlocked = false;
                          if (prevMsg != null) {
                              prevMsg.next = msg.next;
                          } else {
                              mMessages = msg.next;
                          }
                          msg.next = null;
                          if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                          msg.markInUse();
                          return msg;
                      }
                  } else {
                      // No more messages.
                      nextPollTimeoutMillis = -1;
                  }
    
                  // Process the quit message now that all pending messages have been handled.
                  if (mQuitting) {
                      dispose();
                      return null;
                  }
    
                  // If first time idle, then get the number of idlers to run.
                  // Idle handles only run if the queue is empty or if the first message
                  // in the queue (possibly a barrier) is due to be handled in the future.
                  if (pendingIdleHandlerCount < 0
                          && (mMessages == null || now < mMessages.when)) {
                      pendingIdleHandlerCount = mIdleHandlers.size();
                  }
                  if (pendingIdleHandlerCount <= 0) {
                      // No idle handlers to run.  Loop and wait some more.
                      mBlocked = true;
                      continue;
                  }
    
                  if (mPendingIdleHandlers == null) {
                      mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                  }
                  mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
              }
    
              // Run the idle handlers.
              // We only ever reach this code block during the first iteration.
              for (int i = 0; i < pendingIdleHandlerCount; i++) {
                  final IdleHandler idler = mPendingIdleHandlers[i];
                  mPendingIdleHandlers[i] = null; // release the reference to the handler
    
                  boolean keep = false;
                  try {
                      keep = idler.queueIdle();
                  } catch (Throwable t) {
                      Log.wtf(TAG, "IdleHandler threw exception", t);
                  }
    
                  if (!keep) {
                      synchronized (this) {
                          mIdleHandlers.remove(idler);
                      }
                  }
              }
    
              // Reset the idle handler count to 0 so we do not run them again.
              pendingIdleHandlerCount = 0;
    
              // While calling an idle handler, a new message could have been delivered
              // so go back and look again for a pending message without waiting.
              nextPollTimeoutMillis = 0;
          }
      }
    
  • 最后是Handler类

    sendEmptyMessage方法会调用sendMessageDelayed方法,指定target是Handler,最终调用MessageQueue.enqueueMessage放入消息列表中。

      public final boolean sendEmptyMessage(int what)
      {
          return sendEmptyMessageDelayed(what, 0);
      }
    
      public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
          Message msg = Message.obtain();
          msg.what = what;
          return sendMessageDelayed(msg, delayMillis);
      }
    
      public final boolean sendMessageDelayed(Message msg, long delayMillis)
      {
          if (delayMillis < 0) {
              delayMillis = 0;
          }
          return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
      }
    
      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;
          }
          return enqueueMessage(queue, msg, uptimeMillis);
      }
    
      private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
          msg.target = this;
          if (mAsynchronous) {
              msg.setAsynchronous(true);
          }
          return queue.enqueueMessage(msg, uptimeMillis);
      }
    

    post方法,同样会调用sendMessageDelayed方法,只是会指定Message.callback属性。

      public final boolean post(Runnable r)
      {
         return  sendMessageDelayed(getPostMessage(r), 0);
      }
    
      private static Message getPostMessage(Runnable r) {
          Message m = Message.obtain();
          m.callback = r;
          return m;
      }
    

    dispatchMessage方法处理消息回调。

      public void dispatchMessage(Message msg) {
          if (msg.callback != null) {
              handleCallback(msg);
          } else {
              if (mCallback != null) {
                  if (mCallback.handleMessage(msg)) {
                      return;
                  }
              }
              handleMessage(msg);
          }
      }
    
      private static void handleCallback(Message message) {
          message.callback.run();
      }
    
      public void handleMessage(Message msg) {
      }
    

猜你喜欢

转载自blog.csdn.net/chennai1101/article/details/83958256