Use of the bundle for Android messaging--Implementing Object Object Transfer (i)

Source: Internet
Author: User
Tags message queue

UI Update--Message processing massage 

For Android internal message processing, the interface UI to update the operation, can not update the UI directly in the thread. As for why not, when learning Android development, the process in the thread will cause the program to crash.

Why, take a look at the details of many great gods.

Android internal processing data Update UI is time-consuming operation, these time-consuming operating system does not allow in the activity main thread, must operate in other threads, and then send a message to notify the main thread update UI, then we will use multithreading, massage message processing mechanism.

Below we share the introduction of Guo Lin Android Multi-Threading update UI in message processing.

Check it out with a concrete example. Create a new Androidthreadtest project, and then modify the code in the Activity_main.xml as follows:

1 <Relativelayoutxmlns:android= "Http://"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent" >4 <Button5 Android:id= "@+id/change_text"6 Android:layout_width= "Match_parent"7 Android:layout_height= "Wrap_content"8 Android:text= "Change Text" />9 <TextViewTen Android:id= "@+id/text" One Android:layout_width= "Wrap_content" A Android:layout_height= "Wrap_content" - android:layout_centerinparent= "true" - Android:text= "Hello World" the android:textsize= "20SP" /> - </Relativelayout>

Two controls are defined in the layout file, TextView is used to display a Hello world string in the center of the screen, the button is used to change what is displayed in the TextView, and we want to change the string displayed in the TextView to a Nic after clicking the button E to meet. The code in the next mainactivity is as follows:

1  Public classMainactivityextendsActivityImplementsOnclicklistener {2      Public Static Final intUpdate_text = 1;3     PrivateTextView text;4     PrivateButton Changetext;5     PrivateHandler Handler =NewHandler () {6          Public voidhandlemessage (Message msg) {7             Switch(msg.what) {8              CaseUpdate_text:9                 //UI operations can be done hereTenText.settext ("Nice-Meet You"); One                  Break; A             default: -                  Break; -             } the         } -     }; - ... -  + @Override -      Public voidOnClick (View v) { +         Switch(V.getid ()) { A at             NewThread (NewRunnable () { - @Override -                  Public voidrun () { -Message message =NewMessage (); -Message.what =Update_text; -Handler.sendmessage (message);//send the Message object out in                 } - }). Start (); to              Break; +         default: -              Break; the         } *     } $}

Here we first define an integer constant Update_text, which is used to represent the update TextView action. Then add a Handler object and rewrite the parent class's Handlemessage method, where the specific message is processed. If the value of the What field of the Message is found to be equal to Update_text, change the contents of the TextView display to meet. Here's another look at the code in the Click event of the Hange Text button. As you can see, this time we did not directly perform UI operations on the thread, but instead created a Message (Android.os.Message) object and specified the value of its what field as Update_text, and then called Handler's The SendMessage () method sends this message out. Soon, Handler receives this message and processes it in the Handlemessage () method. Note that the code in the Handlemessage () method is now running in the main thread, so we can safely do the UI here. Next, the value of the What field to carry with the Message is judged, and if it equals Update_text, the contents of the TextView display are changed to meet. Now rerun the program and you can see Hello World in the center of the screen. Then click on the Changetext button to display the content is replaced with nice to meet you,

Use the massage message mechanism to handle the UI thread, simple operation. The next step is to parse how the asynchronous message processing mechanism works.

Asynchronous message processing in Android consists of four parts, message, Handler, MessageQueue, and Looper. Where message and handler have been touched in the previous section, and MessageQueue and Looper are new concepts for you, here's a brief introduction to these four sections.

1. Messagemessage is a message that is passed between threads, which can carry a small amount of information inside the thread to exchange data between different threading. In the previous section we used the What field of the Message, in addition to using the Arg1 and Arg2 fields to carry some integral data, using the Obj field to carry an object.

2. Handlerhandler, as its name implies, is the meaning of the processor, which is primarily used to send and process messages. Sending a message is typically using the handler SendMessage () method, and the emitted message is eventually passed to the handler Handlemessage () method after a series of tossing and processing.

3. Messagequeuemessagequeue is the meaning of Message Queuing, which is primarily used to store all messages sent through handler. This part of the message will remain in the message queue and wait for it to be processed. There will only be one MessageQueue object in each thread.

4. Looperlooper is the steward of the MessageQueue in each thread, and after calling the Looper loop () method, it enters an infinite loop, and then whenever a message is found in MessageQueue, it is taken out and passed to Handler in the Handlemessage () method. There will only be one Looper object in each thread.

After understanding the basic concepts of message, Handler, MessageQueue, and Looper, let's comb through the entire process of asynchronous message processing. You first need to create a Handler object in the main thread and override the Handlemessage () method. Then, when UI action is required in a child thread, a message object is created and sent out through Handler. The message is then added to the MessageQueue queue for processing, and Looper tries to remove the pending message from MessageQueue and finally distributes it back to the Handlemessage () method of handler. Since handler is created in the main thread, the code in the Handlemessage () method will also run in the main thread, so we can do the UI operation safely here. The flow of the entire asynchronous message processing mechanism:

After a message has been called by such a process, it goes from a child thread to the main thread, from the inability to update the UI to the ability to update the UI, as is the core idea of the entire asynchronous message processing.

Below share what you have learned to do about the wirelessly of some message processing, the delivery of information. The bundle passes the object objects.

Use of the bundle for Android messaging--Implementing Object Object Transfer (i)

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: 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.