In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 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 use of Handler in Android message mechanism". The content of the explanation in this 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 use of Handler in Android message mechanism".
1. Brief introduction
Handler message mechanism mainly includes: MessageQueue, Handler, Looper, Message.
Message: messages that need to be delivered, and data can be passed
MessageQueue: message queue, but its internal implementation is not a queue to use, but to maintain the message list through the data structure of a single linked list, which has the advantage of inserting and deleting. The main function is to deliver messages to the message pool (MessageQueue.enqueueMessage) and to take messages from the message pool (MessageQueue.next).
Handler: message helper class, the main function is to send various message events to the message pool (Handler.sendMessage) and handle the corresponding message events (Handler.handleMessage)
Looper: message controller, continuous cyclic execution (Looper.loop), reads messages from MessageQueue, and distributes messages to target processors according to the distribution mechanism.
two。 Principle of asynchronous thread switching class LooperThread extends Thread {public Handler mHandler; public void run () {Looper.prepare (); mHandler = new Handler () {public void handleMessage (Message msg) {/ / process incoming messages here}}; Looper.loop ();}}
Each asynchronous thread maintains a unique Looper, and each Looper initializes (maintains) a MessageQueue, then enters an infinite loop to read messages stored in the MessageQueue, and blocks waiting if there is no message.
In the process of instantiating handler, we will first get the looper object of the current thread, and then get the message queue corresponding to the looper object. We can see that the source code Handler holds Looper and MessageQueue.
Private static void handleCallback (Message message) {message.callback.run ();} final Looper mLooper; final MessageQueue mQueue; final Callback mCallback
When we send a message, that is, handler.sendMessage or handler.post, we assign the target in msg to handler itself, which is the essence of message passing from one thread to another, and then added to the message queue.
Private boolean enqueueMessage (MessageQueue queue, Message msg, long uptimeMillis) {msg.target = this; if (mAsynchronous) {msg.setAsynchronous (true);} return queue.enqueueMessage (msg, uptimeMillis);}
We generally override the handlerMessage method to handle messages, which will be called back in the msg.target.disPatchMessage method, thus enabling the transfer of message from one thread to another.
3. Summary
1.Handler is supported by Looper and MessageQueue. Looper is responsible for message distribution and MessageQueue is responsible for message management.
two。 Before creating a Handler, you must first create a Looper,Looper that can exit, but the Looper of the main thread is not allowed to exit.
3. The asynchronous thread Looper,Looper.prepare () creates Looper,Looper.loop () to start polling, and you need to call Looper.myLooper (). Quit () to exit
4.Runnable is encapsulated into Message, which can be said to be a special Message
The thread where 5.Handler.handleMessage () resides is the thread on which the Looper.loop () method is called
The cause of 6.Handler memory leak
Reason: MessageQueue holds Message,Message and activity,delay as long as message holds activity.
Solution: static inner classes, weak references, and finally don't forget to call Handler.removeCallbacksAndMessages (null) to clear all messages.
Public class SampleActivity extends Activity {/ * * Instances of static inner classes do not hold an implicit * reference to their outer class. * / private static class MyHandler extends Handler {private final WeakReference mActivity; public MyHandler (SampleActivity activity) {mActivity = new WeakReference (activity);} @ Override public void handleMessage (Message msg) {SampleActivity activity = mActivity.get (); if (activity! = null) {/ /...} / / MyHandler private final MyHandler mHandler = new MyHandler (this) / * Instances of anonymous classes do not hold an implicit * reference to their outer class when they are "static". * / private static final Runnable sRunnable = new Runnable () {@ Override public void run () {/ * /}}; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); / / Post a message and delay its execution for 10 minutes. MHandler.postDelayed (sRunnable, 1000 * 60 * 10) / / Go back to the previous Activity. Finish ();} @ Override protected void onDestroy () {super.onDestroy (); / / mHandler.removeCallbacksAndMessages (null);}} Thank you for reading. This is the content of "what is the usage of Handler in Android message mechanism?" after the study of this article, I believe you have a deeper understanding of the usage of Handler in Android message mechanism, and the specific usage 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.