Android Study Notes (7) ---- message mechanism in Android

Source: Internet
Author: User

/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail:
* Http://
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/

1. Android management Thread class

1.1 message --- the storage object of messagequeue, information exchanged between threads. After the thread completes Data Processing and needs to update the UI, the message is sent to the UI thread (that is, the main thread)

1.2. messagequeue-a message queue used to store messages sent by handler. Of course, storing messages is not actually a matter of saving. Instead, messages are connected in a linked list, waiting for the logoff to be extracted.

1.3 Logoff-the message pump constantly extracts messages from messagequeue for execution. Therefore, a messagequeue requires a logoff. (That is, there is a message queue in a logoff)

1.4 handler --- acts as the processor and sends new messages to messagequeue and processes the messages, notifying the main thread to update the UI

Briefly describe the Message Passing Process of Android

When the subthread needs to send a message to the main thread, it first needs to create a handler class object for the current thread, and the handler calls the corresponding method to send the message to messagequeue (Message Queue) when logoff finds an unprocessed message in messagequeue, it broadcasts the message. When the handler of the main thread receives the message, the corresponding method is called to process this information and complete the main interface update. (For details about logoff and handler, see points 2nd and 3rd)


A. a thread can have only one loose object. It is a threadlocal object, but a thread or a loose can correspond to multiple handler.

B. Each logoff has a messagequeue (Message Queue). After the loop () method is called, the thread starts to retrieve messages from the queue for execution.

C. logoff converts a thread into a logoff thread.

The approximate architecture of loginthread, message queue, handler, and logint.

2. Create logoff for the thread

In addition to the main thread (or UI thread), the android system will automatically establish a message queue. Of course, logoff is also established. If other threads are used, the android system does not automatically create a message queue for them. You must manually create a message queue. Once a logoff is created, the Message Queue inside the logoff is automatically created.

2.1 main thread

When the Android system starts the main thread (ui thread), the application automatically enters the main function of the thread, and then automatically creates a logoff for the main thread.

The Process Code is as follows:

Logoff. preparemainlooper (); // creates a message queue and sets the master looperlooper. loop (); // start the message loop before it can run // The two pieces of code are in the main function of the main thread.

2.2. Other threads

You must manually create other logoff threads.

The specific method is as follows:

Write the following code in the run method of other threads:

Public void run () {logoff. prepare (); // create a message queue/** this is the core code of the thread. You can customize the code/logoff. loop (); // start the message loop}

To view the Loopers of the current thread, call loopers. myloopers ();

Other logoff Methods

2.2.1. logoff. mylogoff () to get the logoff object of the current thread.

2.2.2. logoff. getthread.

2.2.3. the logoff. Quit () method ends the logoff loop.

3. Handler usage

The first thing we should know is handler's asynchronous message processing. asynchronous refers to sending messages at the same time as processing messages.

First, handler sends a message to the message queue in other threads, and the Function Method for sending the message returns immediately. In addition, in the thread for creating the handler, logoff extracts the message from the message queue and handler calls the method to process the message.

A common example is as follows:

Create a handler in the main thread, and then send a message to the handler in other long-time threads. At the same time, the handler's processing function is in the main thread, handler processes the sent message and updates the UI. (Because the system has automatically created a looper in the main thread (that is, the main thread is also a looper thread), you do not need to manually create Looper)

When handler is created, it is associated with a logoff. By default, it is associated with the Logoff of the current thread. Of course, you can also set it yourself.

Since a thread can have multiple handler (Handler has only one logoff), we can create multiple handler in the same thread, such

Public class looperthread extends thread {private handler myhandler1; private handler myhandler2; @ override public void run () {// The current thread changes to the looper thread Looper. prepare (); // instantiate two handler myhandler1 = new handler (); myhandler2 = new handler (); // start to cyclically process Message Queue logoff. loop ();}}

Send message:

Handler can send messages in any thread as follows.

,postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message,
,sendMessageDelayed(Message, long)

The runnable object and message are sent. In fact, Handler has encapsulated the runnable object as a message. Therefore, handler sends messages.

The handler information is included when the message is sent. That is to say, you can know which handler it is from a message object. Therefore, when logoff executes the message, it will know which handler sent the message.

For example, define a message msg

Then the message.tar get method returns the handler object that sends the message.

Message Processing:

Handler processes messages in (associated with itself) threads. There are two main methods. (A thread can have multiple handler)

void    dispatchMessage(Message msg) //Handle system messages here.void      handleMessage(Message msg)   //Subclasses must implement this to receive messages.

Message Processing means notifying the main thread to update the UI.

Related Article

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: 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.