Parsing and using of Handler
1.handler is a mechanism of multi-thread communication in android.
In @ 1android, only the UI thread (the main thread) is allowed to operate or change the UI, and other threads cannot operate on the UI.
@ 2 other threads need to refresh the UI, so you have to tell the UI thread that Handler is used to process the message in the handeMessage method.
@ 3handler can be defined in the main thread or in other threads, and its function is different.
@ 4 classes related to handler execution: Handler,Message,MessageQueue,Looper
UI thread: the main thread, which initializes a Looper object automatically upon creation, and also creates a message queue
Message: messages, including message ID, message processing objects and processed data, etc., which are uniformly queued by MessageQueue and finally processed by Handler.
Handler: processor, responsible for sending and processing Message. When using Handler, you need to implement the handleMessage (Message msg) method to handle specific Message, such as updating UI, and so on.
MessageQueue: message queue, which is used to store messages sent by Handler and execute according to FIFO rules. Of course, storing the Message is not a practical preservation, but a linked list of Message, waiting for Looper extraction.
Looper: a message pump that constantly extracts Message from MessageQueue for execution. Therefore, a MessageQueue needs a Looper.
Thread: thread, which is responsible for scheduling the entire message loop, that is, the execution place of the message loop.
The use of 2.handler
@ 1 is used in the main thread
-1-define Handler objects, override handleMessage methods, process messages in methods, and update UI
Private Handler mHanlder = new Handler () {public void handleMessage (android.os.Message msg) {numberAdapter = new BlackNumberAdapter (getApplicationContext (), mList, mBnd); lv_blacknum.setAdapter (numberAdapter);};
-2-send messages in other threads, sometimes you need to define the message object yourself
MHanlder.sendEmptyMessage (BLACKNUM_PREPARED)
@ 2handler is defined in the child thread. You need to create your own Looper object.
-process:
= 1. Call Looper.prepare () to create a Looper object for the current thread, and the MessageQueue message queue is automatically created; 2. Create Handler object, override handleMessage method; 3. Call the Looper.loop () method to start the Looper message loop