In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces what is the relationship between Handler,MessageQueue and Looper in Android, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
When we talk about the message mechanism of Android, we naturally think of Handler. We know that Handler is the upper interface of the Android message mechanism, so we only need to interact with Handler in the development process. Many people think that the function of Handler is to update UI, which is true, but there are many other uses in addition to updating UI,Handler. For example, we need to perform time-consuming Icano operations in child threads. It may be to read some files or to access the network, when the time-consuming operation is completed, we may need to make corresponding changes on the UI, but due to the limitations of the Android system, we cannot update the UI control in the child thread, otherwise it will report an exception, at this time Handler can come in handy, we can switch to the main thread through Handler to perform the UI update operation.
Here are some common methods of Handler:
Void handleMessage (Message msg): the method of handling messages, which is usually overridden.
Final boolean hasMessages (int what): detects whether the message queue contains messages with the specified value of the what attribute.
Message obtainMessage (): the method to get the message, which has multiple overloaded methods.
SendEmptyMessage (int what): sends an empty message.
Final boolean sendEmptyMessageDelayed (int what, long delayMillis): specifies the number of milliseconds before an empty message is sent.
Final boolean sendMessage (Message msg): send the message immediately.
Final boolean sendMessageDelayed (Message msg, long delayMillis): specifies how many milliseconds to send the message.
Final boolean post (Runnable r): performs the runnable operation.
Final boolean postAtTime (Runnable r, long upTimeMillis): performs a runnable operation at a specified time.
Final boolean postDelayed (Runnable r, long delayMillis): specifies the number of milliseconds before the runnable operation is performed.
After introducing the method, let's start with a simple example and analyze it step by step:
Public class MainActivity extends AppCompatActivity {public static final int MSG_FINISH = 0X001; / / create an anonymous inner class of Handler private Handler handler = new Handler () {@ Override public void handleMessage (Message msg) {switch (msg.what) {case MSG_FINISH: LogUtils.e ("id of handler is->" + Thread.currentThread (). GetName ()); break;}; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_main); / / start the time-consuming operation consumeTimeThread (findViewById (R.id.tv)); / / handler.post ()} / / start a time-consuming thread public void consumeTimeThread (View view) {new Thread () {public void run () {try {LogUtils.e ("Name of the time-consuming child thread is-- >" + Thread.currentThread () .getName ()); / / run Thread.sleep (2000) on the child thread / after completion, send the download completion message handler.sendEmptyMessage (MSG_FINISH);} catch (InterruptedException e) {e.printStackTrace ();}}. Start ();}}
Running result:
The above example is actually the basic use of Handler, creating a Handler object in the main line, and then sending a message to inform the Handler of the main thread to perform the corresponding operation by simulating a time-consuming operation in the child thread and sending a message through the sendEmptyMessage (int) method. From the running results, we can also know that Handler is indeed running on the main thread.
So the question is, how do messages sent through Handler get to the main thread? Next, let's break the mystery. High energy ahead, please pay attention! To better understand how Handler works, let's first introduce several components that work with Handler:
The object on which the Message:Handler receives and processes messages.
Looper: there can be only one Looper per thread. Its loop method is responsible for reading the message in MessageQueue and sending it to Handler for processing.
MessageQueue: message queue, which manages Message on a first-in-first-out basis. When a program creates a Looper object, it creates a MessageQueue object in its constructor.
Handler: it has two functions-sending messages and processing messages, the program uses Handler to send messages, and messages sent by Handler must be sent to the specified MessageQueue;, otherwise the messages will not be saved in MessageQueue. The MessageQueue is managed by Looper, which means that if you want Handler to work properly, you must have a Looper object in the current thread. As long as we have a general understanding of the above components, we will analyze them in detail later. Since the message is sent from Handler, let's start with Handler. Let's first take a look at the source code of the construction method of Handler:
Public class Handler {/ * unimplemented empty method handleMessage () * / public void handleMessage (Message msg) {} / * one of the constructors we usually use to create Handler * / public Handler () {this (null, false) } / / the concrete implementation of this (null, false) called within the constructor public Handler (Callback callback, boolean async) {/ / checks whether the Handler is static. If not, it may lead to memory leak if (FIND_POTENTIAL_LEAKS) {final Class
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.