1. Relationship between messagequeue and message
Message Chinese is the smallest unit of Message Processing and thread processing. It contains a processed dataset or an operation, and tells the destination what to do. Each messagequeue contains a message. Each message is not directly inserted into messagequeue, but through messagequeue. idlehandler and looper work together, put the message into messagequeue, and addidlehandler (messagequeue. idlehandler handler) and removeidlehandler (messagequeue. idlehandler handler) method, put messagequeue. idlehandler is pushed into messagequeu.
2. Relationship between thread and handlerthread
Handlerthread is a thread with a loose loop.
3. logoff Introduction
Logoff is a loop. It must be bound to a fixed thread. The getthread () method can be used to obtain the thread object bound to the logoff, and getmainlogoff () can be used to obtain the logoff object in the main thread, myloue () can get the loageobject in the current thread, and get the messagequeue object in the current thread through myqueue.
4. Relationship between thread, handlerthread, and logoff
It is generally declared that a thread does not carry a logoff loop, but it can be declared through logoff. the prepare () method adds logoff; logoff to a thread. loop () executes the messagequeue in the thread bound to the loopger until the loopger ends. Loop. quit () ends the loop. The instance code is as follows:
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(); } }
Fortunately, Android provides us with a method to directly generate a loose thread and instantiate a handlerthread object. The loose object of the handlerthread can be obtained through the handlerthread. getlooper () method.
5. Relationship between handler and thread handlerthread low.message messagequeue
The handler class is used to send and process messages/runnable. A handler class is associated with a unique thread, and the thread is the thread that generates the handler,
It sends the message to the messagequeue in the thread that generates the message, and then executes the message when the message is out of the queue.
post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long)Is used to process runnable objects;
sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long), AndThe sendmessagedelayed (message, long) method is
It is used to process message objects. It is generally executed immediately, but we can set latency and execute it cyclically.
You can use the handlemessage (Message MSG) method to receive messages.
Instantiation: Handler)