In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the role of Handler in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the role of Handler in Android"?
In terms of performance optimization, the UI operation of Android is not thread safe, which means that if multiple threads operate UI components concurrently, it will lead to thread safety problems. To solve this problem, Android provides a solution: specify a simple rule that only allows UI threads to modify components in Activity.
When the first program starts, Android starts a Main Thread (main thread) at the same time, which is responsible for processing UI-related time, such as key time, screen contact and screen drawing events, and distributes the related time to the corresponding components for processing. Therefore, the main thread is also called the UI thread
The message passing mechanism of Android is a form of "event handling" inside and outside, which is to solve the multithreading problem of Android applications: only UI threads are allowed to modify U components in Activity, which will cause newly started threads to be unable to dynamically change the property values of interface components. But in the actual development, especially in the game development involving animation, it is necessary to let the newly started thread change the properties of the interface component periodically, which requires the message passing mechanism of Handler to realize.
The underlying implementation principle of Handler
To understand the implementation principle of handler, the most important thing is the implementation principle of Looper, and Looper is the core of the implementation of handler mechanism.
When any handler uses sendMessage or post, it first constructs a Message, puts itself into the Message, and then puts the Message into the MessageQueue,Looper of the corresponding Looper to obtain the handler or runnable of the message execution by controlling the MessageQueue.
To perform the specified operation of handler in the current thread, you must first see if there is looper in the current thread. If there is a looper,handler, it will pass sendMessage, or post will first construct a message, and then put the message into the looper of the current thread. The looper will loop out the message in the current thread for execution. If there is no looper, you must build a looper in the current thread through the looper.prepare () method, and then actively execute looper.loop () to implement the loop.
Two functions of the Handler class: sending information in the newly started thread and obtaining and processing information in the main thread
Message objects processed and received by Message:Handler
Looper: each thread has a Looper, and its loop is responsible for reading the message in the MessageQueue. After reading the message, it gives the message to the Handler processing of the message.
MessageQueue: message queue, which manages Message on a first-in-first-out basis. When a program creates a Loop, it creates a MessageQueue object in its constructor
In the main UI thread, the system is initialized; a Loop object, so the Handler can be created directly
In the child thread started by yourself, you must create a Looper object, start it, create the object, and call its prepare method
Thank you for your reading, the above is the content of "what is the role of Handler in Android". After the study of this article, I believe you have a deeper understanding of the role of Handler in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.