Message, a carrier of information, used to pass data to handler.
Handler (Handler processor, is the main processor of the message, responsible for the delivery of the message, the execution of the message content processing)
Sends and processes the message and Runable objects, which are associated with the MessageQueue of a thread. Each thread instance is associated with a separate thread and the MessageQueue of that thread.
Handler is bound together with the thread that created it, and the message and Runable objects are passed to MessageQueue, and Handler is responsible for executing them when they leave the MessageQueue.
The handler has two main uses:
(1) Determine the execution of one or more message and runnable objects at a later point in time.
(2) Some of the actions to be performed are queued in other threads (not Handler bound threads).
Post (Runnable): Runnable executes on handler bound thread, that is, does not create a new thread.
Postattime (Runnable,long):
Postdelayed (Runnable,long):
Sendemptymessage (int):
SendMessage (Message):
Sendmessageattime (Message,long):
Sendmessagedelayed (Message,long):
SendMessage This action queues the Message object, which contains some information, and handler's Hanlermessage (message) handles the message.
Handlermessage (Message) must be overridden by the Handler subclass.
Post This action puts the Runnable object into the Messagequeue,messagequeue when you receive these messages, execute them in a certain sort of order.
When posting or sending to a hanler, you can have three behaviors: when the MessageQueue is ready to process, define a delay time, define an exact time to process. The latter two allow you to implement Timeout,tick, and time-based behavior.
When a new process is created, the main thread comes with a MessageQueue, which manages the top-level app object and the form created by the main thread.
MessageQueue: (Message queue, used to store messages published through Handler, followed by FIFO execution.) )
The underlying class that contains the message list, Looper is responsible for distributing the message. The message is not added directly to the MessageQueue, but is associated to the Looper by Messagequeue.idlehandler
You can get MessageQueue from the current thread by Looper.myqueue ()
Each message queue will have a corresponding Handler. Handler will pass to MessageQueue.
Two ways to send a message: SendMessage or post. Both messages will be inserted at the end of the message queue and
Follow the FIFO execution. However, the messages sent by these two methods differ slightly in the way they are executed: by sendMessage
Sends a Message object, which is handled by the Handler handlemessage () function, and by the post side
Method sends a Runnable object, it executes itself.
Looper:
The Looper class is used to execute a message loop in a thread. Call prepare () in the thread to create a Looper, and then use Loop () to process the messages until the loop terminates.
(Looper is a bridge of communication between Handler and message queues, and the program component first passes the message through Handler to
Looper,looper the message into the queue, it can also get messages in MessageQueue, generate message queue, pass to handler)
Most of the interactions with message loops are through handler.
The following is a typical threading implementation with Looper.
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 ();
}
}
Relationship between message, Handler, message Queue, Looper in a single-threaded model