Android Asynchronous Message Processing Mechanism (1) basic use of Handler

Source: Internet
Author: User

Android Asynchronous Message Processing Mechanism (1) basic use of Handler

The Android UI is thread-safe. If you try to perform UI operations in the Child thread, the program may crash. The solution should be to create a Message object, and then use Handler to send it out, and then get the Message object just sent in Handler's handleMessage () method, then, the UI operation will not crash again.
This processing method is called an asynchronous message processing thread. To put it simply, update the UI in the Child thread.

Handler basic usage

Handler can be used in two ways:

Sends messages from the worker thread to the main thread (UI thread) and processes messages in the main thread; from the main thread (UI thread) to the worker thread) send a message to process the message in the Child thread. The subthread sends messages to the main thread.

The main steps are as follows:

Customize the Handler (non-abstract class) subclass in the main thread and implement handleMessage(Message)In worker Thread sendMessage(Message)The Handler object puts the Message in the MessagQueue of the Message Queue; The loiter retrieves the Message from the Message queue and finds the corresponding Handler object. Logoff calls the Handler object's handleMessage(Message)Method to process the Message.

Here, we only need to complete Step 1 and step 2, and the rest is done by the system.

The main code is as follows:

// Create the Handler Object handler = new MyHandler () in main Thread; // create worker Threadnew Thread (new Runnable () {@ Override public void run () {// create the Message object message = handler. obtainMessage (); message. obj = I am a message sent from worker Thread to main Thread; // the handler that sends the message object. sendMessage (message );}}). start ();

The MyHandler class is as follows:

Class MyHandler extends Handler {@ Override public void handleMessage (Message msg) {Log. d (result:, Thread. currentThread (). getName (); // you can operate the UI Log only in the main Thread. d (result, (String) msg. obj );}}

The preceding method enables the subthread to send messages to the main thread (UI thread) and process messages in the main thread.

The main thread sends messages to the subthread.

The main steps are as follows:

1. prepare the logoff object 2. generate a Handler object in worker Thread and implement the 'handlemessage () 'method; 3. in MainThread, the Handler object's 'sendmessage (Message) 'method is used to send a Message.

The implementation code in the subthread is as follows:

New Thread () {@ Override public void run () {// prepare the logoff object logoff. prepare (); handler2 = new MyHandler (); // check the message queue cyclically and call the handleMessage () method for processing. If no message object exists, the thread is blocked. Logoff. loop () ;}}. start ();

The above MyHandler class is still used here. Then the handler2'ssendMessage(Message)Method to send messages to the Child thread and process the messages in the Child thread. The Code is as follows:

Message message = handler2.obtainMessage (); message. obj = I am a message sent from the main Thread to the worker Thread; handler2.sendMessage (Message );

The preceding method enables the master thread (UI thread) to send messages to the worker thread and process messages in the main thread.

That is, where the Handler object is created, the Handler object is processed.

 

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.