How Android asynchronously updates the UI using the handler messaging mechanism

Source: Internet
Author: User

Because of the performance requirements, Android requires that the UI be updated only in the UI thread, and to update the UI in other threads, let's introduce a way to use the handler messaging mechanism.

Here's how to update a TextView:

 Packagecom.example.runonuithreadtest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView TV; Handler Handler=NewHandler () { Public voidhandlemessage (android.os.Message msg) {if(msg.what==0x123) {Tv.settext ("Updated TextView"); }   }; }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);   Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); NewMyThread (). Start ();} classMyThreadextendsThread {@Override Public voidrun () {//Two-second delay update   Try{Thread.Sleep (2000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } handler.sendemptymessage (0x123); } } } 

Of course, the performance test of the app, I am more commonly used this platform: www.ineice.com

How Android asynchronously updates the UI using the handler messaging 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.