SoFunction
Updated on 2025-04-04

The difference and connection between android

Nowadays, most people are familiar with the handler. There are only two scenarios where we often use handler:

1. Asynchronously update the UI

2. Delay task

But I have always been confused, which is what unique function does this method have?

By looking at the source code, it was found that the post method converts task r into a message and puts it into the messageQueue message queue in the thread where the handler is located, and it is a message sent immediately, so it is neither asynchronous nor delayed, so the problem is:

1. What is the difference between it and sendMessage()?

2. What is its unique function?

Before we draw a conclusion, we will analyze it through the source code:

/**
    * Causes the Runnable r to be added to the message queue.
    * The source code comment here means: add the task object r to the message queue.
    */
  public final boolean post(Runnable r)
  {
    return sendMessageDelayed(getPostMessage(r), 0);
  }
/**
    * Enqueue a message into the message queue after all pending messages
 * Then we will look at the method of sending delay message directly in the post method. The source code comment* means to put this message into the message queue.
    */
  public final boolean sendMessageDelayed(Message msg, long delayMillis)
  {
    if (delayMillis < 0) {
      delayMillis = 0;
    }
    return sendMessageAtTime(msg, () + delayMillis);
  }
/* Finally, let's look at another method called in the post. There are no comments in the source code, but we can easily see that this method is to wrap the task r into an empty message and return */
private static Message getPostMessage(Runnable r) {
    Message m = ();
     = r;
    return m;
}

At this point, we can conclude:

There is no difference between it in essence. Both send a message to the message queue, and the message queue and handler depend on the same thread.

Next, we will analyze it based on the examples in actual applications:

Use sendMessage and post to complete the same asynchronous update of the UI:

private TextView tv_up;
  private String new_str = "";
  /*post method solves UI update problem handler creation method*/
  private Handler handler_post = new Handler();
  /*sendMessage method solves UI update problem handler creation method*/
  Handler handler_senM = new Handler() {
    public void handleMessage(Message msg) {
      if ( == 1) {
        /*The operation of updating the UI by sendMessage method must be completed in the handleMessage callback of the handler*/
        tv_up.setText(new_str);
      }
    };
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    new Thread(new Runnable() {
      @Override
      public void run() {
        new_str = "Update UI";
        /*sendMessage method solves the problem of sending messages to handler (handler in main thread)*/
        handler_senM.sendEmptyMessage(1);
        /*post method solves UI updates and completes the update operation directly in runnable. This task will be added to the message queue of the handler thread, that is, the message queue of the main thread*/
        handler_post.post(new Runnable() {
          @Override
          public void run() {
            tv_up.setText(new_str);
          }
        });
      }
    }).start();
  }

From this code we can see:

Post and sendMessage are just differences in usage, and there is no difference in essence.

Final summary:

1. There is no difference between post and sendMessage in essence, but there is a little difference in actual usage.

2. Post has no unique function. Post is essentially implemented using sendMessage. Post is just a more convenient usage in one place.

Summarize

The above is the difference and connection between android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!