Looper and Handler class analysis

Source: Internet
Author: User

I. Looper

1. How do I use Looper?

(1) define a looperthread.

Class Looperthread extends Thread {

Public Handler Mhandler;

public void Run () {

Looper.prepare (); Call prepare ();

Looper.loop (); Enter the message loop.

}

}

(2) Use Looperthread in the application:

{

New Looperthread (). Start (); Start a new thread, and the thread function is run.

}

2. Looper.prepare () function:

public static final void prepare () {

if (sthreadlocal.get () = NULL) {

throw new RuntimeException ("Only one Looper could be created per thread");

}

Sthreadlocal.set (New Looper ()); Constructs a Looper object that is set to the calling thread's local variable (TLV) and binds the looper to the thread.

}

3. Creation of Looper:

Private Looper () {

Mqueue = new MessageQueue (); Constructs a message queue.

Mrun = true;

Mthread = Thread.CurrentThread (); Gets the thread object for the current threads.

}

Thus, in the call to the prepare thread, a Looper object is set for the thread, and a message queue is encapsulated inside the Looper object.

4. Implementation of Looper.loop ()

public static final void loop () {

Looper me = Mylooper (); Mylooper () returns the Looper object that is saved in the calling thread TLV.

MessageQueue queue = Me.mqueue;

while (true) {

Message msg = Queue.next ();

if (msg! = NULL) {

if (Msg.target = = NULL) {//target type is handler type, if empty, it means that the message loop needs to be exited.

Return

}

Msg.target.dispatchMessage (msg); Call the handler of the message and give it the DispatchMessage function to handle.

Msg.recycle;

}

}

}

5. The relationship between Looper,message and handler:

There is a message queue in Looper that stores the message to be processed.

There is a handler in the message that this handler uses to process the message.

Two. Handler

1. The handler class contains three members:

Final MessageQueue Mqueue; Will eventually point to a looper message queue

Final Looper Mlooper; It is possible for its value to be passed through the constructor from outside, and it is possible to get the looper of the calling thread through the Looper.mylooper () function.

Final Callback Mcallback; Called as a callback.

2. Handler provides a series of functions for creating messages and inserting message queues. Functions such as sending a message:

Public Final Boolean sendMessage (Message msg) {

Return sendmeeagedelayed (msg, 0);

}

Public Final Boolean sendmessagedelayed (Message msg, long Delaymillis) {//Delaymillis is the relative time based on the current time

if (Delaymillis < 0) {

Delaymillis = 0;

}

Return Sendmessageattime (MSG, systemclock.uptimemillis () + Delaymillis); Call Sendmessageattime to calculate the current time.

}

Public Final Boolean sendmessageattime (Message msg, long Uptimemillis) {///Uptimemillis is absolute time, so the function deals with absolute time

BOOL sent = false;

if (mqueue! = null) {

Msg.target = this; Set target to itself, because handler also encapsulates the interface for message processing.

Sent = Mqueue.enqueuemessage (msg, uptimemillis);

}

return sent;

}

3. Function DispatchMessage () to process messages

public void DispatchMessage (Message msg) {

if (msg.callback! = null) {//If the message itself has callback, it is passed directly to the callback handle of the message.

Handlecallback (msg);

} else {

if (mcallback! = null) {///severed handler set Mcallback, it is processed by it.

if (Mcallback.handlemessage (msg)) {

Return

}

}

Handlemessage (msg); Finally, we give the subclass processing.

}

}

Looper and Handler class analysis

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.