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