-
-
-
Android messaging mechanism
- The Message class
- Handler Common method of the Handler class
-
-
Android messaging mechanism:
In Android, the thread on which the UI page resides is called the main thread. If the main thread executes an operation that takes more than five seconds, the Application will report an ANR (Application Not Responding) error. However, time-consuming operations are unavoidable, such as loading network images, loading network data, reading and writing large files, etc. In this case, time-consuming operations are usually carried out in the sub-thread, and the sub-thread sends data or results to the main thread after completion. This is where Android messaging, or communication between Androids, is used.
Each time an Android application starts a thread, it creates a Message Queue for it and then enters an infinite loop. Then constantly check to see if there are new messages in the queue that need to be processed. If not, the thread goes to sleep, and if not, the message is distributed. Android has three core classes for Message processing: Looper,Handler, and Message. Message Queue is wrapped in Looper. We don’t deal with Message Queue directly, so we don’t use it as a core class. Looper: a message can be stored in a MessageQueue (MessageQueue) and executed in an infinite loop. Looper: a message can be stored in a MessageQueue and executed in an infinite loop
The Message class:
The main function of Android.os. Message is to encapsulate messages and specify the operation form of messages. Variable or method Type Description *public int WHAT variable is used to define what operation this Message belongs to *public Object obj variable is used to define the information data this Message passes public int arg1 variable GetTarget () usually gets the Handler object that operates on the messageCopy the code
Handler class common methods:
The Message object encapsulates all the messages that need to be handled by the Android.os.handler classCopy the code
Method Type Description Public Handler() construct creates a new Handler instance. Public Handler(Looper Looper) construct creates a new Handler instance with the specified queue. Public final ObtainMessage (int what, int arg1) Public final Message obtainMessage(int what, int arg1) Public void handleMessage(Message MSG) Public void handleMessage(Message MSG) Public Final Boolean hasMessages(int what) Public Final Boolean hasMessages(int what) Void removeMessages(int what) Delete the specified Message public final void removeMessages(int what, SendEmptyMessage (int WHAT) sendEmptyMessage(int what) Sends an empty Message public Final Boolean sendEmptyMessageAtTime(int what, Public Final Boolean sendEmptyMessageDelayed(int what, Long delayMillis) Wait a specified period of time before sending a Message. Public Final Boolean sendMessage(Message MSG) Send a Message
Why Handle and Message are used? Handle can Handle time-consuming tasks, but the main thread cannot. The child thread cannot control the main thread to manipulate data. The Message object encapsulates all messages and needs handle to send them