Android message loop mechanism

Source: Internet
Author: User
Tags message queue

In addition to the UI thread (the main thread) in Android, the worker threads created by default have no message loops and message queues. If you want the thread to have Message Queuing and message loops and have a message-handling mechanism, you will need to call Looper.prepare () in the threads first to create the message queue, and then call Looper.loop () to enter the message loop.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/5C/wKiom1Xhgg2ighfwAAJ6JuNn5hc083.jpg "title=" 1.png " alt= "Wkiom1xhgg2ighfwaaj6junn5hc083.jpg"/>

Class Looperthread extends Thread {

Public Handler Mhandler;

public void Run () {

Looper.prepare ();

Mhandler = new Handler () {

public void Handlemessage (Message msg) {

Process incoming messages here

}

};

Looper.loop ();

}

}

The relationship between UI thread, Worker Thread, Handler, massage Queue, Looper can be clearly displayed:

Several basic concepts in the interpretation:

1.Message

A Message object, as the name implies, is the class that records message information. There are several more important fields for this class:

A.arg1 and Arg2: We can use two fields to hold the integer value we need to pass, and in the service we can store the service ID.

B.obj: This field is an object type, and we can let the field pass a number of items to the recipient of the message.

C.what: This field can be said to be a message flag, in the message processing, we can according to the different values of this field to do different processing, similar to when we handle the button event, through the switch

(V.getid ()) determines which button is clicked. When using the message, we can create a message instance from the new message (), but Android recommends that we pass Message.obtain () or handler.obtainmessage () Gets the message object. This is not necessarily a straightforward creation of a new instance, but rather a message instance that is immediately available from the pool of messages, and is then fetched and returned to this instance. Conversely, if there is no message instance available in the pool, a new message object is given according to the parameter. By analyzing the source, it is known that the Android system instantiates 10 message objects in a messaging pool by default.

2.MessageQueue

Message Queuing, which holds the data structure of the message object and stores the message according to the "FIFO" principle. Storage is not a meaningful save, but instead the message object is concatenated in a linked list. MessageQueue objects do not need to be created by ourselves, but have looper objects to manage them, and a thread can have at most one MessageQueue. We can get the MessageQueue in the current thread through Looper.myqueue ().

3.Looper

MessageQueue manager, in a thread, if there is a Looper object, there must be a MessageQueue object, and there is only one Looper object and one MessageQueue object. If the Looper object exists in our thread, we can get it through Looper.mylooper (), and we can get the Looper object of the main thread in the current application via Looper.getmainlooper (). One thing to note in this place is that if the Looper object is in the main thread of the application, Looper.mylooper () and Looper.getmainlooper () 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), and when MessageQueue loops to the message, The Handlemessage () method of the handler object corresponding to the message object is called to process it. Because the messages are processed in the Handlemessage () method, we should write a class that inherits from handler and then handles the operations we need in Handlemessage (). When 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.


This article from "Tech record" blog, declined reprint!

Android message loop 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.