Message model in Android (Message,messagequeue,handle,looper,)
Message Communication in Android
1.Android the application mechanism of the middle thread?
1) All time-consuming operations on Android should be performed on the worker thread.
2) All UI actions in Android should be performed on the main thread (UI thread).
FAQ?
1) The main thread executes a time-consuming operation, okay? Not good, this will block UI operations.
2) Work after the time-consuming operation, if there is data to be passed to the main thread, how to achieve?
What is the message model for multithreaded applications in 2.Android?
The purpose of using the message model in Android is to try not to block
The main thread, and then give the user a more friendly experience.
The message model in Android focuses on objects:
1) Message: The carrier of the data
2) MessageQueue: Store Multiple Message objects
3) Looper: Iterate the message queue and take out the message.
4) Handler: Send, Process message in message queue.
The message model in Android focuses on object pseudo-code implementations:
Class message{
Object obj; data
int what; What data to use to do
.....
}
Class messagequeue{
Object[] Message=new object[5];
public void put (Message msg) {}
Public Message Tabke () {}
}
Class looper{
Private MessageQueue Msgq;
public void loop () {
while (true) {...}
}
}
Class handler{
public void SendMessage (Message msg) {}
public void Handlemessage (Message msg) {}
}
Message model application case in 3.Android?
1) The worker thread sends a message to the main thread?
2) The main thread sends a message to the main thread?
3) The main thread sends a message to the worker thread?
Remember:
1) The main thread has created the Looper and Message Queuing objects by default at startup
2) to whom to send messages let handler associate who looper.
3) Handler Handlemessage method runs on which thread by handler
The associated Looper.
Summary of methods related to message model objects in
4.Android?
1) Message
Carrier for data
(obj data,
What data to do,
obtain () to get a message,....)
2) MessageQueue stores multiple Message Objects
3) Looper (Prepare () creates a Looper and binds to the current thread,
Mylooper () obtains the current thread Looper,
Loop (),
Getmainlooper () Default association current thread Looper,
Quit ())
4) Handler (SendMessage message to WHO, depending on Handler associated Looper,
Handlemessage can perform a message processing operation, SENDXXXXX)
1 The main thread sends a message to the main thread
1, sends a message to the main thread via the main thread
message Classification: System message, user-defined message
The main thread sends a message to the main thread handler object.
|--> When we send a custom message, handler needs us to build it ourselves.
|--> A thread may have many handler objects associated with it
The main thread sends messages that are stored in the main thread's MQ. The manager of the message queue in the
main thread is the looper of the main thread.
|--> This looper contains a blocking infinite loop.
|--> The purpose of this loop is to traverse and looper the associated MQ object
|--> This looper is responsible for getting the message from MQ and handing the message to handler.
FAQ:
1) When is the Looper object of the main thread created? When the system starts
this Looper object is bound to the current line approached (with Threadlocal)
2) When is the MQ in the main thread created? MQ is created automatically when the Looper object is created.
3) When was the MQ custom message created? How do I create it?
|--> is created when needed.
|--> creation method?
Message msg=message.obtain ();
Message msg=handler.obtainmessage ();
4) When is the handler object in the main thread created?
|--> is created when needed.
How do I create |-->?
Anonymous inner class: New Handler () {overriding Handlemessage method}
building Handler Subclass
2 Child threads send messages to the main thread
Scenario: Get the Handler object for the main thread in a child thread
How to obtain:
Build the Handler object on the main thread.
Build the main thread handler object in the child thread??
Handler=new handler (Looper.getmainlooper ());
3 How child threads create Looper objects
1. What are the reasons for creating looper in a child thread?
No Looper objects in child threads by default
A handler object in a child thread that needs to use a child thread to send a message
2. How do child threads create Looper? Automatically build MQ when Looper is created
Looper.prepare ();//build process? It is taken from the current thread first, and if none is created. Binds to the current thread after creation.
... Build Handler
Looper.loop ();
FAQ:
A. Handler of child threads requires looper of child threads
B. The handler object sending message for the child thread is the MQ that is sent to the child thread
C. Child threads cannot update the main thread interface while processing messages.
4 main thread How to get the handler object for a child thread
Scenario: Try to get the Looper object for the child thread in the main thread.
Message model in Android (message,messagequeue,handle,looper)