Detailed explanation and example of Android message queue model

Detailed explanation and example of Android message queue model

The message queue and message loop of Android system are for specific threads. A thread can exist (of course, it can not exist), a message queue and a loop. In Android, except for UI thread (main thread), the created worker thread has no message loop and message queue by default. If you want the thread to have message queues and message loops and have a message handling mechanism, you need to call Looper.prepare () first in the thread to create the message queue, then call Looper.loop () to enter the message loop. As shown in the following code:

In this way, the thread has a message processing mechanism. If loop. Prepare() is not called to create a message queue, an error of "Java. Lang. runtimeException: can't create handler inside thread that has not called loop. Prepare()" will be reported.

The following figure clearly shows the relationship among UI thread, worker thread, handler, mass queue and looper:

Explain some basic concepts in the figure above:

1.Message

Message object, as its name implies, is a class that records message information. This class has several important fields:

a. Arg1 and arg2: we can use two fields to store the integer value we need to pass. In service, we can use them to store the service ID. b. Obj: this field is of type object. We can pass a certain number of items to the receiver of the message. c. What: this field can be said to be the flag of the message. In message processing, we can conduct different processing according to different values of this field, which is similar to us judging which button is clicked through switch (v.getid()) when processing the button event.

When using message, we can create a message instance through new message(), but Android recommends that we get the message object through message. Obtain() or handler. Obtainmessage(). This does not necessarily mean to directly create a new instance, but first check whether there is an available message instance in the message pool. If there is one, directly take out and return the instance. Conversely, if there is no available message instance in the message pool, a new message object will be created according to the given parameter. By analyzing the source code, we can know that the Android system instantiates 10 message objects in the message pool by default.

2.MessageQueue

Message queue is used to store the data structure of message object and store messages according to the principle of "first in first out". Storage is not a practical storage, but a concatenation of message objects in the form of a linked list. Messagequeue objects do not need to be created by ourselves, but are managed by looper objects. A thread can only have one messagequeue at most. We can get the messagequeue in the current thread through looper. Myqueue().

3.Looper

The manager of messagequeue. If there is a looper object in a thread, there must be a messagequeue object, and there is only one looper object and one messagequeue object. If there is a looper object in our thread, we can get it through looper. Myloop(). In addition, we can also get the looper object of the main thread in the current application system through looper. Getmainlooper(). One thing to note here is that if the looper object is located in the main thread of the application, looper. Myloop () and looper. Getmainloop () get the same object.

4.Handler

The processor of the message. Through the handler object, we can encapsulate the message object, and then add the message object to the messagequeue through SendMessage (MSG); When the messagequeue loops to the message, it will call the handlemessage () method of the handler object corresponding to the message object to process it. Since the message is processed in the handlemessage () method, we should write a class that inherits from the handler and then process the operations we need in handlemessage ().

In addition, we know that the Android UI operation is not thread safe, so we can't update the UI in the child thread. However, Android provides several methods to notify the UI thread to update the interface in the child thread:

More commonly, the handler is used to receive the data sent by the sub thread through the handler, and use this data to update the UI with the main thread. So, as long as the Handler object is created in the main thread, the Handler's sendMessage method is invoked in the sub thread, and the message is put into the message queue of the main thread, and the handleMessage method of the handler will be called in the Handler main thread to process the message.

In the above code, after the handler is created in the main thread, the child thread can send the message to the main thread through the SendMessage () method and process it in the handlemessage () method.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>