The part that runs in the main thread can use handler directly, because Looper.preparemainlooper () is called during the main thread startup (Activitythread's main function). The Looper class also directly defines a static Looper instance Smainlooper is used to hold the looper of the main thread, which can be obtained by means of a statically method. Therefore, any code snippet that runs in the main thread can be directly new Handler () without binding looper,messagequeue, because it is already bound. ^ ^^ ^^
The Looper.prepare () method binds the current thread to an Looper instance and stores it in treadlocal with only one Looper object. In Looper, there is a MessageQueue, the message queue, handler can send messages to the queue by means of SendMessage. A thread can have more than one handler,handler bound to the Looper object, and the message queue in it, and the Looper.loop () method loops through the message in MQ, and the caller sends its handler to handle it. 2, Looper implementation principle the Looper class contains a Message Queuing object and a thread object. When Looper is created, a message queue is automatically created and the internal thread object is pointed to the thread that created the Looper. When the Looper is turned on (Looper.loop ()), it automatically enters the infinite for loop, continually traverses the message queue, and if no message is blocked, a message callback handler's Handlemessage method for processing. 3, Looper.prepare () first, to use the Looper mechanism typically creates a handler object in the current thread, which automatically creates a Looper object and message queue, which is a message queue that belongs to the current thread space. At this point, however, the looper does not traverse or bind to the current thread. Within the Looper object, an empty Message Queuing object and a null thread are also included. With the Looper.prepare () method, the message queue is first pointed to the message queue of the current thread, and the empty thread also points to the current thread. Thus the binding is implemented. from:http://www.nowcoder.com/questionterminal/4f88dfd8ec97432f80308031e73507c1?orderbyhotvalue=0&query= Android&pos=33
"Turn" Android Looper understand