Logoff class in Android

Source: Internet
Author: User

The logoff class in Android is a class used to encapsulate message loops and message queues. It is used to process messages in Android threads. Handler can be seen as a tool class used to insert messages to a message queue.

(1) The logoff class is used to enable a message loop for a thread.
By default, the new thread in Android does not enable the message loop. (Except for the main thread, the main thread system automatically creates a logoff object for it to enable message loop .)
The logoff object stores messages and events through messagequeue. A thread can have only one logoff corresponding to one messagequeue.

(2) The Handler object is usually used to interact with logoff. Handler can be seen as an interface of logoff to send messages to a specified logoff and define the processing method.
By default, handler is bound to the loose of the thread in which it is defined. For example, if handler is defined in the main thread, it is bound to the loose of the main thread.
Mainhandler = new handler () is equivalent to new handler (loler. myloler ()).
Looper. mylooper (): gets the looper object of the current process. Similar Looper. getmainlooper () is used to obtain the looper object of the main thread.

(3) New handler () directly in a non-main thread will report the following error:
E/androidruntime (6173): uncaught handler: thread-8 exiting due to uncaught exception
E/androidruntime (6173): Java. Lang. runtimeexception: can't create handler inside thread that has
Not called logoff. Prepare ()
The reason is that no logoff object is created by default in the main thread. You must first call logoff. Prepare () to enable logoff.

(4) logoff. Loop (); let logoff start to work, get the message from the message queue, and process the message.

Note: Write it in logoff. the code after loop () will not be executed. This function should be a loop. When mhandler is called. getlooper (). after quit (), the loop will be aborted, and the subsequent code can be run.

(5) Based on the above knowledge, the main thread can send messages to subthreads (non-main threads.

Declare the mhandler in the following example as a class member and send a message through mhandler in the main thread.

Logoff is used to process messages in the android thread. By default, a thread is not bound to any logoff. When we call lorule. Prepare (), if the current thread is not bound to any lorule, A lorule will be created to bind it to the current thread. When we call logoff. loop (), IT processes The logoff messages corresponding to the current thread, retrieves messages from the message queue, processes messages, and loops until it calls quit () function.

Note: logoff. loop () is a while loop, which only calls the quit () function, loit. loop () functions can be completed, and subsequent code can be run. A thread corresponds to a loose and a loose corresponds to a message queue messagequeue.

For handler, the message is actually sent to the messagequeue of its corresponding logoff message queue. The final processing is still carried out in the while loop of logoff. Loop. A logoff can be used to construct multiple handler. Because of logoff. the loop () function is a while loop that will allow the current thread to process the loop until the quit () function is called for the Logoff of the thread. Therefore, if you want to send a message to the handler or add runnable for transaction processing, either in another thread or in the runnable () Run () during transaction processing in the function.

Note: Handler constructor handler () and handler (handler. callback callback), although there is no logoff parameter, but it actually uses logoff. mylogoff () to obtain the logoff in the current thread.
The following is a typical loginthread implementation in Android APIs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.