In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to apply Message mechanism in Android". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to apply Message mechanism in Android" can help you solve the problem.
1. Message mechanism of inter-thread communication
1. Message introduction
Frameworks\ base\ core\ java\ android\ Os\ Message.java
Message is the carrier of transmitting information between threads, including the description of the message and arbitrary data objects. Message contains two additional int fields and an object field, so that in most cases, users no longer need to do memory allocation work. Although the constructor of Message is public, * uses the Message.obtain () or Handler.obtainMessage () function to get the Message object, because the implementation of Message includes a recycling mechanism that can provide efficiency.
2. MessageQueue introduction
Frameworks\ base\ core\ java\ android\ Os\ MessageQueue.java
MessageQueue is used to accommodate Message queues, where the Message is distributed by Looper, and Message cannot be added directly to the MessageQueue, but through the Handler associated with Looper.
3 、 Looper
Frameworks\ base\ core\ java\ android\ Os\ Looper.java
Looper is used by threads to run message loops. The thread itself does not have a message loop, so you need to call the perpare function in the thread, and then call loop to process the message. In Android, the system automatically creates a Looper for the main thread when it starts.
Create the Looper for this thread:
Public static final void prepare ()
The Looper running this thread:
Public static final void loop ()
Get the Looper of this thread:
Public static final Looper myLooper ()
Get the Looper of the main thread:
Public synchronized static final Looper getMainLooper ()
4 、 Handler
Frameworks\ base\ core\ java\ android\ Os\ Handler.java
Handler is used to send and process Message and Runnable objects associated with threads. Each Handler is associated with a separate thread and thread message queue. When you create a Handler, Handler is bound to threads and thread message queues, and since then, Handler can send Message and Runnable to the corresponding message queues and process messages coming out of the message queues.
Handler provides most of the interfaces that users need to call
A. Create a Handler:
Create a Handler associated with the local thread:
Public Handler ()
Public Handler (Callback callback)
Create a Handler associated with the specified thread:
Public Handler (Looper looper)
Public Handler (Looper looper, Callback callback)
B, create a message
Public final Message obtainMessage () public final Message obtainMessage (int what) public final Message obtainMessage (int what, Object obj) public final Message obtainMessage (int what, int arg1, int arg2) public final Message obtainMessage (int what, int arg1, int arg2, Object obj)
C, delete message
Delete messages that are not processed in the message queue
Public final void removeMessages (int what) public final void removeMessages (int what, Object object)
D, send a message
Public final boolean sendMessage (Message msg) public final boolean sendMessageDelayed (Message msg, long delayMillis) public final boolean post (Runnable r) public final boolean postDelayed (Runnable r, long delayMillis)
E, processing messages
In the loop function of Looper, the messages in the received message queue of MessageQueue are looped out, and then the dispatchMessage function of Hander is called to process the messages. As for how to handle (the corresponding messages), it is up to the user to specify (three methods, priority from high to low: Callback in Message, an object that implements the Runnable interface, in which the run function does the processing work. The mCallback in Handler points to an object that implements the Callback interface, and the handleMessage in it processes it; the class corresponding to the Handler object for handling messages inherits and implements the handleMessage function, and handles messages through the handleMessage function of this implementation.
Public void dispatchMessage (Message msg) {if (msg.callback! = null) {handleCallback (msg);} else {if (mCallback! = null) {if (mCallback.handleMessage (msg)) {return;}} handleMessage (msg);}}
II. The use of the Message mechanism
The Message mechanism has two uses:
Scheduled execution: delay the execution of message scheduling to the specified time
Thread communication: queue some operations in other threads
1. Timing execution
Delay the execution of the message to a specified time through the Handler of this thread, which is equivalent to the function of a timer
Public final boolean sendMessageDelayed (Message msg, long delayMillis) public final boolean postDelayed (Runnable r, long delayMillis)
You can also cancel execution through removeMessages before the specified time expires
2. Thread communication
Android's UI is single-threaded. Android hopes that the UI thread can give User a quick response. If the UI thread spends too much time doing things behind the scenes, after 5 seconds, Android will give an error message. So to avoid delaying UI, the more time-consuming work should be left to a separate thread to perform. But if the behind-the-scenes thread executes the UI object, Android sends an error message, so the UI thread needs to communicate with the behind-the-scenes thread. The UI thread distributes the work to the behind-the-scenes thread, and the behind-the-scenes thread executes and returns the corresponding state to the UI thread, allowing the UI thread to update the UI accordingly.
Conclusion: the so-called message mechanism is actually very simple, and it takes only four steps to implement it:
1. Instantiate Looper (because a looper is required to instantiate Handler)
2. Instantiate Handler. Here you need to override the handleMessage method to process the received messages.
3. Instantiate the Message object, call the obtainMessage method of the instantiated handler object, pass the data to the obtainmessage method, and the obtainMessage method will instantiate a Message object. (you can also send objects that implement the Runnable interface here)
4. Call the sendMessage method of Handler to send the instantiated Message object. The process is the same for each thread, as long as you follow the four steps above to send and receive messages.
This is the end of the introduction on "how to apply the Message mechanism in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.