Brief analysis of Android message model

Source: Internet
Author: User

Android Summary Series

I. Message system elements and fundamentals

L Message Queuing

L Send a message

L message Read

L Message Distribution

L message loop Thread

The message system must rely on a message loop thread to poll its own message queue, and if a message comes in, it invokes the message handler function, which is processed according to the message type and its parameters. Message system to work, there must be news generation and consumption. The thread that generates the message is called the producer thread, and the thread that consumes the message is called the consumer thread. The producer thread sends the message to the message queue, and the consumer thread takes the message out of the message queue and processes it accordingly. When Message Queuing does not have a message, the consumer thread goes into a suspended state, and when a new message arrives, the consumer thread wakes up and continues to run. Of course, producers can also be consumers.

Ii. Fundamentals of the message model

The Android messaging system uses the concepts of message, MessageQueue, Looper, Handler, and so on, from the basic principles of the messaging system to know that these are conceptual wrappers, and that the essential thing is the way messages are distributed in Message Queuing. Android cleverly uses the object abstraction technology to abstract the concept of looper and handler, and on the basis of Looper and handler concept, through the view processing framework, it is perfect to realize the function of message distribution. The following is a detailed introduction to message, MessageQueue, Looper, handler.

1) Message

A message is a carrier of information objects, messages. Messages are usually placed in message queues. Typically when you create a message instance object, you use the static method obtain () in the message class, which has multiple overloaded versions to choose from.

When you use the obtain () method to get a message, you do not create a new instance directly, but you first view the message pool with a message instance that is available, if one is directly reused, or if not, create a new/message instance. Call Removemessage () to remove the message from the message queue and put it into the messages pool. In addition to using the obtain method to get Messaeg, you can also obtain it through the Obtainmessage method of the handler object.

2) MessageQueue

MessageQueue is a data structure that has a first-in-one-out feature for storing message queues. Each thread has a maximum of one MessageQueue. The Looper object is typically used to manage the MessageQueue of a thread. When you create the main thread, the Android application creates a Looper object by default, and the other threads do not automatically create the Looper, but you can call Looper's prepare () function to create it when needed.

3) Looper

Looper is the manager of MessageQueue. Each MessageQueue cannot be separated from Looper and Looper objects are created through the prepare function. Looper first creates a message loop framework, then creates a message queue and hangs it in the thread context of Linux, entering a loop that takes messages and distributes the messages. Each of the Looper objects must be associated with a thread. The Looper object of the current thread can be obtained by calling Looper's Mylooper (). For Android. Looper detects if the MessageQueue has a message, some of it broadcasts it, and it corresponds to the handler received, called the Handlemessage () method.

4) Handler

Handler is the core of the messaging system, and handler's role is to add messages to a specific (Looper) message queue and distribute and process messages in that message queue. When constructing handler, you can specify a Looper object that is created with the looper of the current thread if not specified. For detailed implementation please refer to Looper source code.

Handler

In this diagram, handler is created in Uithread.

I drew this picture myself,

In this figure Handlethread is declared in the Handlerthread sub-thread Handler;handler is handler declared in the main thread of the Uithread, you can visually see how the child thread and the main thread to send messages, that is, in the ' The handler in the Uithread is obtained in the sub-thread, and the Handlemessage method is used to perform the operation in the main thread by handler sending the message to the main thread. In turn, the same is true for messages sent to child threads in the main thread.

A few more articles, will be combined with the source code, a detailed description of the example.

Brief analysis of Android message model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.