One of the handler used in Android

Source: Internet
Author: User

Handler is a mechanism for messaging in Android, and is typically used for updates to the UI thread. We know that the child thread, which is not the UI thread, is not able to update the UI, so this is going to make handler great.

For example, here is a simple child thread of the updated UI case, remember: The following case is wrong

XML code:

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 Android:paddingbottom= "@dimen/activity_vertical_margin"6 Android:paddingleft= "@dimen/activity_horizontal_margin"7 Android:paddingright= "@dimen/activity_horizontal_margin"8 Android:paddingtop= "@dimen/activity_vertical_margin"9 Tools:context= "Com.example.android_handler1." Mainactivity " >Ten  One     <TextView A         Android:id= "@+id/textview" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "@string/hello_world" /> -  - </Relativelayout>

Java code:

1  PackageCom.example.android_handler1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.widget.TextView;6 7  Public classMainactivityextendsActivity {8     PrivateTextView TextView =NULL;9 @OverrideTen     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -TextView =(TextView) Findviewbyid (R.id.textview); -         NewThread () { the              Public voidrun () { -                 Try { -Thread.Sleep (1000); -}Catch(interruptedexception e) { +                     //TODO auto-generated Catch block - e.printstacktrace (); +                 } ATextview.settext ("Updata"); at             }; - }.start (); -     } -  -}

When we run this code, we will find that the program will flash back, the reason is that the child thread to update the UI, this is the wrong way. Usually we use handler to notify the main thread, the UI thread, to update the UI.

So all we need to do is change the code to this:

1  PackageCom.example.android_handler1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.Handler;6 ImportAndroid.widget.TextView;7 8  Public classMainactivityextendsActivity {9     PrivateTextView TextView =NULL;Ten     PrivateHandler Handler =NewHandler (); One @Override A     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); theTextView =(TextView) Findviewbyid (R.id.textview); -         NewThread () { -              Public voidrun () { -                 //use handler to pass a post, allowing the main thread to update the UI +Handler.post (NewRunnable () { -                      + @Override A                      Public voidrun () { at                         //TODO auto-generated Method Stub -                         Try { -Thread.Sleep (1000); -}Catch(interruptedexception e) { -                             //TODO auto-generated Catch block - e.printstacktrace (); in                         } -Textview.settext ("Updata"); to                     } +                 }); -  the             }; * }.start (); $     }Panax Notoginseng  -}

One of the handler used in Android

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.