Android Learning Note: Handler message delivery processing mechanism

Source: Internet
Author: User

First say why you need handler:

The main thread of Android is also called the UI thread, which handles UI -related time. Android only allows UI components to be modified in the main thread, and when we need to modify the UI interface in other threads, we need to send a message to the main thread, The main thread then handles the UI modification events.

perform a time-consuming operation or network operation in the UI thread, or the ANR ("Application Not Responding") exception will be thrown or Networkonmainthreadexceptionwill exception.

At these times, we use Handler for message delivery and processing.

Here is a description of the Handler class:

(1) Two main functions:

1. Send a message in a non-main thread.

2. Get and process messages in the main thread.

(2) Several components that work together

Message:Handler received and processed messages object

Looper: Each thread has a Looperthat is responsible for managing MessageQueue. Its loop method continuously extracts the Message from the MessageQueue and gives Handler processing.

MessageQueue: A message queue that is managed by Looper and manages messages in a FIFO manner . a MessageQueue object is created in the Looper constructor .

(3) Handler method for sending and handling Message :

void Handlemessage (Message msg): A method for handling messages that needs to be rewritten by itself.

Boolean hasmessages (int what, Object object): Checks whether the message queue contains The What property is the specified value and the object property is a message for the specified object.

Boolean hasmessages (int): Checks whether the message queue contains A message with the What property is the specified value.

Message Obtainmessage (): Gets the message.

Boolean sendemptymessage (int): Sends an empty message.

Boolean sendemptymessageattime (int what, long Uptimemillis): Sends an empty message at the specified time.

Boolean sendemptymessagedelayed (int what, long Delaymillis): Sends an empty message after some time.

Boolean sendMessage (Message msg): Messages are sent.

Boolean sendmessageatfrontofqueue (Message msg): The message is added to the front of the message queue.

Boolean sendmessageattime (Message msg, long Uptimemillis): Sends a message at a specified time.

Boolean sendmessagedelayed (Message msg, long Delaymillis): Sends a message after some time.

(4) Handler message processing mechanism :

The developer needs to rewrite the message handling method of the Handler class, but when the newly started thread sends the message, the message is sent to the MessageQueue associated with it ,Handler messages are constantly fetched and processed from MessageQueue, at which point the message processing method is called.

(5) Use Handler to handle UI modifications that need to be made in other threads :

The Android system creates a message queue and message loop (Looper) for Activity when it starts . In order to handle the UI processing requirements in the non-main thread , we first send the message in the new threads and then rewrite the Handler 's handlemessage () in the main path method to implement UI modification requirements.

Android Learning Note: Handler message delivery processing mechanism

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.