Android thread communication tool-Handler

Source: Internet
Author: User

Handler is an interface provided by Android for inter-thread communication.

I didn't pay attention to it at first, because handler is used by the main thread, that is, the UI thread, and the main thread has a message loop by default.ProgramIs transparent.

Therefore, I mistakenly thought that handler is useless like a Linux signal.

Linux signal processing programs are very limited. We recommend that you set a flag in the processing program and process it in other loops.

Even using printf in a processing program may lead to a deadlock in a single-threaded program!

For Android, if a non-UI thread uses handler to receive messages, it must explicitly call loop. Prepare ();

To process messages, you need to provide your own loop. Loop (); this is not the SB design like the Linux signal! This is in line with human thinking!

Supplement

Logoff made me so disappointed. The loop () method is an endless loop. Users cannot know when and under what conditions to exit the loop.

I don't know when to call this quit () method, because I don't know whether the message queue has been processed.

In the game loop is not available, so you have to implement a queue of 1v1 on your own. Fortunately, the operation is completely normal.

If logoff can provide interfaces such as Win's peekmessage, it is better to hand over the loop to the user's own writing, with more control room.

It seems that the idlehandler is provided, and the callback is empty for the message queue. If you have time, replace it. You can use the one you have written.

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.