Android Handler mechanism principle

We know that android also has multi-threaded development, including the main thread (main thread) and sub-threads. Usually we call the main thread the UI thread, which is the interface thread.
Usually we may encounter a situation in our development: we open a child thread to update UI components, which will cause the program to throw an exception, such as: Only the original thread that created a view hierarchy can touch its views, in other words, only the main thread Only threads can update the UI.
At this point, the reader will think that the sub-thread cannot update the UI, then you are very wrong. Another way of thinking, since it cannot be updated directly, it should be possible to update indirectly, yes, very correct! At this time, we need to use Handler to achieve this requirement.

First, the principle of thread

We start an android application. By default, a UI thread, a message queue and a poller Looper will be generated. Looper continuously queries the messages in the message queue. If it is not empty, it will be taken out to the handler processor for processing. .

Second, the core principle of the Handler mechanism

Here's a look at it in the form of a picture:
write picture description here

If you think this blog is well written, please give it a thumbs up

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325874576&siteId=291194637