Android handler mechanism

Source: Internet
Author: User


This blog is the author in reading <android kernel stripping >, feeling and write. Readers are strongly advised to read this book.
To say handler mechanism, handler is not the pig's foot. The real pig's foot is the Android asynchronous message processing process.

Asynchronous Message Processing thread:
The generic thread ends when run () is executed. The asynchronous message processing thread is a while (true) loop. The message is constantly being removed from the current thread, and messages are constantly being processed. If there is no message, it blocks, waiting for the queue to have new messages to continue processing.


Scenario using asynchronous Message Processing threads:
1. Tasks need to be resident, such as events that handle user interactions. The typical UI interaction in Android is
2. Tasks require specific processing based on externally transmitted information.

General implementation ideas for asynchronous message processing threads:

1. Each asynchronous thread contains a message queue (messagequenue). The general principle of Message Queuing is FIFO. The first incoming message is processed first.
2. The thread's body is a while (true) loop, constantly fetching messages from the current thread, invoking the callback of the message object to execute the corresponding function to process the message.
3. Other external threads can send messages to this thread. Message Queuing inside a thread must be locked in a read-write message.

Almost all operating systems implement their own asynchronous message processing threads. Now let me see how Android is implemented. Specifically, the three steps above are done.

Coarse granularity explains the approximate process of the Android asynchronous message processing thread.

A thread can contain one or more handler to send a message to the messagequenue of the thread thread to which the handler is a member. The only loop message that is inside the thread is polling the Message Queuing message, and the message is retrieved by the message's Handlermessage (). callback method
Process the message.


The main implementation here is the Handler Loop Message messagequenue with these four related classes.

Loop (Message polling)
A thread if it is to be an asynchronous message processing thread. You must call Loop.prepare (). This method internally gives the loop class's threadlocal variable (that is, the thread-level variable) Sets a Loop object. The Lopp constructor creates a Messagequenue object for the thread.
Note here that Loop.prepare () can only be called once in the thread. Because the method indirectly creates a Messagequenue object. There can be only one message queue within a thread.
Loop also has one method, loop (). is to let the loop start polling the message queue for processing messages.

The two methods mentioned above detail the <android kernel stripping >p 44.

Messagequenue

This class is Message Queuing. The message is processed in a queued manner. That is, the principle of FIFO. Of course, if the message itself indicates the event that the message is executing, you must wait until that moment to process the message. Messages in Message Queuing are represented by message
The message is not stored inside the Messagequenue class. Instead, the message is stored in the underlying C code. To increase efficiency.
Messagequenue There are two main ways to remove and write messages. Next () Enquencemessage (). The implementations of both methods use JNI techniques. The <android core stripping >p 44.

Handler

Sending a message to a message queue and how it is handled is done through that class. Note that handler only sends messages to the messagequence of the message queue on which it is located. Each Message object has a handler variable target pointing to the handler object that sent it out .
Loop uses this variable to find the corresponding handler callback with its handmessage () to process the message.

Through the description of the above three classes I believe that everyone on the Android asynchronous message processing thread, handler mechanism must have a deep understanding of it. From now on, my mother never worries that I will not be with the new UI, I will not use handler.


Android handler mechanism

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.