Difference and relation between Android handler.post and handler.sendmessage
Now most people are familiar with handler. There are two common scenarios for handler:
1. Asynchronously update UI
2. Delayed tasks
But I always have a puzzle, that is, what is the unique function of handler. Post (R)?
By looking at the source code, it is found that the post method converts the task r into a message and puts it into the messagequeue message queue in the thread where the handler is located, and it is an immediate message, so it is neither asynchronous nor delayed, so the problem comes:
1. What is the difference between it and sendmessage()?
2. What is its unique function?
Before concluding, we will analyze the source code:
So far, we can conclude:
Handler.post and handler.sendmessage are essentially the same. They both send a message to the message queue, and the message queue and handler depend on the same thread.
Next, we will analyze it in combination with examples in practical application:
Use SendMessage and post to complete the same task of asynchronously updating the UI:
From this code, we can see:
Post and SendMessage are only differences in usage, but there is no difference in essence.
Final summary:
1. There is no difference between post and SendMessage in essence, but there is a difference in actual usage
2. Post also has no unique function. In essence, post is implemented with SendMessage. Post is just a more convenient usage
summary
The above is the difference and contact between Android handler.post and handler.sendmessage introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!