Android thread asynchronous message processing mechanism (i)

Source: Internet
Author: User

Android does not allow UI manipulation in child threads, but sometimes we have to perform some time-consuming tasks in the child threads and then update the corresponding UI controls based on the results of the task's execution. For this scenario, Android provides a set of asynchronous message processing mechanisms that perfectly address the problem of UI manipulation in child threads.

Main thread:

1. New Handler Object

2. Override the Parent class Handlemessage method, where the UI operation is performed, and the code for this method is in the main thread.

Child Threads:

1. Create a Message object

2. Specify what field of message

3, call handler's SendMessage method, send the message out

Examples are as follows:

Mainactivity.java:

1      PackageCom.junhao.updateui;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.os.Message;7 ImportAndroid.view.View;8 ImportAndroid.view.View.OnClickListener;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.TextView; One  A  Public classMainactivityextendsActivityImplementsOnclicklistener { -      -      Public Static Final intUpdate_text = 1; the     PrivateTextView text; -     PrivateButton Changetext; -      -     PrivateHandler Handler =NewHandler () {//New Handler Object +          Public voidHandlemessage (Message msg) {//override the Parent class Handlemessage method, where the UI operation is performed, and the code for this method is in the main thread.  -             Switch(msg.what) { +              CaseUpdate_text: AText.settext ("Good-bye"); at                  Break; -             default: -                  Break; -             } -         } -     }; in  - @Override to     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the          *Text =(TextView) Findviewbyid (R.id.textview); $Changetext =(Button) Findviewbyid (r.id.changetext);Panax NotoginsengChangetext.setonclicklistener ( This); -     } the      +      Public voidOnClick (View v) { A         Switch(V.getid ()) { the          CaseR.id.changetext: +             NewThread (NewRunnable () {//Creating child Threads -                  Public voidrun () { $Message message =NewMessage ();//Create a Message object $Message.what = Update_text;//Specify what field of message -Handler.sendmessage (message);//call Handler's SendMessage method to send the message out -                 } the }). Start (); -              Break;Wuyi         default: the              Break; -         } Wu     } -}

Activity_main.xml:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Tools:context= "${relativepackage}.${activityclass}" >6 7     <TextView8         Android:id= "@+id/textview"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content" One Android:text= "@string/text"  A android:layout_centerinparent= "true"/> -      -     <Button the         Android:id= "@+id/changetext" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "@string/btn"/> +  - </Relativelayout>

Strings.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4     <stringname= "App_name">UpdateUI</string>5     <stringname= "text">How are you doing</string>6     <stringname= "BTN">Update UI</string>7 8 </Resources>

Android thread asynchronous message processing mechanism (i)

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.