Java Official website Description of the Looper object:
public class Looperextends Object
Class used to run a message loop for a thread. Threads By default don't have a message loop associated with them; To create one, call prepare () in the thread, the "is" to run the loop, and then loop () to have it process messages until the The loop is stopped.
Most interaction with a message loop is through the Handler class.
This is a typical example of the implementation of a looper thread, using the separation of prepare () and loop () to create An initial Handler to communicate with the Looper.
Copy Code code as follows:
Class Looperthread extends Thread {
Public Handler Mhandler;
public void Run () {
Looper.prepare ();
Mhandler = new Handler () {
public void Handlemessage (msg) {
Process incoming messages here
}
};
Looper.loop ();
}
}
Main methods:
The
static void Loop (): Run the message queue in this thread. The
static void prepare (): Initialize The current thread as a looper.