Android.os.handler (EXT)

Source: Internet
Author: User

Android.os.handler related Knowledge collation

Handler is responsible for sending and processing messages on Android. Its main uses are:

1) Send messages on a schedule or execute a runnanble (using the Post method), 2) Send messages from other threads into the message queue, avoid thread collisions (common in updating the UI thread)/To add an action to a queue that does not belong to its own thread

Some ways to distribute messages in handler
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnable Long)
Sendemptymessage (int)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)

The Post class method above allows you to arrange a runnable object into the main thread queue,
The SendMessage class method allows you to schedule a message object with data to queue and wait for updates.

By default, handler accepts a message loop instance under the current thread (using theHandler (Looper Looper),Handler (Looper Looper, Handler.callback Callback) can specify threads, while a message queue can be distributed by multiple objects in the current thread, Processing (in the UI thread, the system already has an activity to deal with, you can take a number of handler to handle). When instantiating handler, Looper can be any thread, as long as there are handler pointers, any thread can sendmessage. Handler handling of message is not concurrent. A looper will read the next one only after processing one message, so the processing of the message is blocked (the Handlemessage () method should not have a time-consuming operation that can place time-consuming operations on other threads. Send a message after the operation (via the Sendmessges method), and then update the UI by Handlemessage ().

When the application starts, Android first turns on a main thread (that is, the UI thread), the main thread is the UI control in the management interface, for event distribution, for example, if you click on a button,android will distribute the event to the button, in response to your action. If you need a time-consuming operation at this time, for example: network read data, or read a large local file, you can not put these operations in the main thread, if you put in the main thread, the interface will appear suspended animation phenomenon, if 5 seconds has not been completed, will receive an error on the Android system "forced shutdown", this time we need to put these time-consuming operations in a sub-thread, because the child thread involves UI updates, the Android main thread is not safe, that is, the update UI can only be updated in the main thread, Operations in a child thread are dangerous. At this point, because handler is running in the mainline approached (UI thread), it and the child thread can pass the data through the message object, and at this time, the handler assumes the Message object (which contains the data) that receives the child thread. Put these messages into the main thread queue and update the UI with the main thread.

Android.os.handler (EXT)

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.