Talk Android together now (one hundred and seventy fourth back: Android's Handler mechanism c)

Tell me what you were Hello, everybody, let's say on a gyrus is an example of Android in the Handler mechanism, this time we went to this example. Gossip Hugh mentioned, words Reformed turn. Let's Talk Android now!

Tell me who, we are on a gyrus demonstrates how to send and receive messages to use Hanlder mechanism through concrete examples, this gyrus we analyze the source code, mainly to see the hidden principles behind it to achieve.

We take a look Handler class, it is located in frameworks/base/core/java/android/os/Handler.javaa file. It has multiple constructors, we do them one analysis, its main function is to give members of the variable assignment, we look at the focus of two member variables commonly used:

  • final Looper mLooper; // this is a message queue manager
  • Looper final MessageQueue mQueue; // This is the message queue

Their assignment is carried out by the constructor, the constructor is overloaded, which we achieve a look, and omits other exception processing details:

 public Handler(Callback callback, boolean async) {
     mLooper = Looper.myLooper();
     mQueue = mLooper.mQueue;
 }

You can see from the source code, they are assigned by Looper implemented, only to be explained here, we will introduce a detailed analysis Looper class in later chapters gyrus.

Next we look at Handler is a commonly used method, first of all look at its handleMessagemethods, its source code is as follows:

   /**
     * Subclasses must implement this to receive messages.
     */
    public void handleMessage(Message msg) {
    }

We can see that this is a null method, but also shows the comment, subclass needs to receive messages in order to implement it. There is also a call dispatchMessagemethods also need to look at, Loooper is through it to the callback handleMessagemethod, the following is its source code, the source code can also see this:

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

Finally, we look at the method of sending messages, each method to achieve similar, we only list the function prototypes and functional, detailed source code is no longer listed:

 public final boolean sendMessage(Message msg)                            //发送消息到消息队列中
 public final boolean sendEmptyMessage(int what)                          //发送空消息到消息队列中
 public final boolean sendMessageDelayed(Message msg, long delayMillis)   //延迟发送消息到消息队列中
 public final boolean sendEmptyMessageDelayed(int what, long delayMillis) //延迟发送空消息到消息队列中
 public final void removeMessages(int what)                               //删除消息队列中的消息
 public final boolean hasMessages(int what)                               //判断消息队列中是否有消息

The method of transmitting a message over a private ultimately by a method enqueueMessageto add messages to the message queue, the source code is as follows:

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
     msg.target = this;
     if (mAsynchronous) {
         msg.setAsynchronous(true);
     }
     return queue.enqueueMessage(msg, uptimeMillis);
 }

Tell me, on Android Handler mechanism in case we described here, and there are any examples For, Let's hear next decomposition!

Published 528 original articles · won praise 131 · views 620 000 +

Guess you like

Origin blog.csdn.net/talk_8/article/details/105173097