Android (Java) Learning Note 134:handler Usage summary and stopwatch case

Source: Internet
Author: User
Tags message queue

First, the definition of handler:
The handler mainly receives the data sent by the child threads and updates the UI with the main thread to interact with the UI main thread。 For example, you can use handler to send a message, and then in the handler thread to receive, process the message, to avoid directly in the UI main thread to handle the transaction caused by the main thread of the UI other processing work, Android provides handler as the main thread and child threads of the link You can also pass the handler object to another process to send events through handler in other processes, and you can delay the processing of some transactions by sending a message with the delay of handler.
Typically, when an application starts, Android first opens a main thread (the UI thread), and the main thread is the UI control in the admin interface for event distribution。 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, if 5 seconds has not been completed, you will receive an Android system error message Force Close.
This time we need to put these time-consuming operations on a sub-thread, because the child threads involve UI updates, but when there are actions in the child thread that involve manipulating the UI, it can be dangerous to the main thread-that is, the update UI can only be updated in the main thread, and operating in a child thread is dangerous. At this time, handler appeared to solve this complex problem, because handler runs in the mainline approached (UI thread), it and the child thread can pass the data through the Message object, at this time, the handler undertakes to accept the child thread to pass over ( The child thread passes the message object with the Sedmessage () method (which contains data), puts the messages into the main thread queue, and updates the UI with the main thread.

Ii. Some features of handler
Handler can distribute the Message object and the Runnable object into the main thread, each handler instance is bound to create his line approached (typically in the main thread), that is, after the handler object is initialized, the default is bound to the message queue of the process it initializes, Therefore, you can make use of the message queue contained in handler to establish some sequence of operations.

Iii. Some methods of distributing messages in handler
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnable Long)
The Post class method allows you to arrange a runnable object into the main thread queue
Sendemptymessage (int)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)
The SendMessage class method allows you to schedule a message object with data to queue and wait for updates.

Iv. Application Examples:
1, pass the message. Used to accept data sent by a child thread and update the UI with this data in conjunction with the main thread.
In Android, actions for the UI typically need to be placed in the main thread. If there is an action on the UI in the child thread, then it is necessary to send the data message to the message queue as a messages object, then use the Handlermessge method in handler to process the transmitted data information and manipulate the UI. The Class SendMessage (Message msg) method implements the operation that sends the message. The Handlemessage method that is overridden when initializing the handler object to receive Messgae and to perform related operations.
2. Pass the Runnable object. Used for Message Queuing that is bound by handler to schedule the execution of different operations.
When the handler object is initialized, the message queue is automatically bound by default. The class post method enables you to send a Runnable object to a message queue and execute the Run method in the different Runnable objects sequentially, in the same way as the queue mechanism.
In addition, Android CPU allocation of the smallest unit is a thread, handler is generally created in a line thread, so handler and thread are bound to each other, one by one. While Runnable is an interface, thread is a subclass of runnable. So, they're both a process.

Android (Java) Learning Note 134:handler Usage summary and stopwatch case

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.