1. The Android UI is unsafe when the thread is not secure, and if the UI is updated in a child thread , an exception occurs, causing the program to crash.
in order to solve these problems, our common practice is to use Android 's asynchronous messaging mechanism can be implemented (create a message object, send it using Handler , and then in Handler 's handlemessage () method to get the message that you just sent Message object, and then the UI action here). So it is necessary to understand the mechanism of asynchronous message Looper, Handler, message and other principles.
Here is a sample to use, and then through the source analysis it.
public class mainactivity extends activity { public static final int update_text = 1; private textview Text; private button changetext; private handler handler = new handler () { @Override public void handlemessage (message msg) { switch (msg.what) { case update_text : text.settext ( "Nice yo see you again"); break; default: break; } }}; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); text = ( TextView) findviewbyid (r.id.text); changetext = (Button) findviewbyid (r.id.change_text);// Changetext.setonclicklistener (This); Changetext.setonclicklistener (New onclicklistener () { @Override       &NBsp; public void onclick (View view) { switch ((View.getid ())) { case R.id.change_text: new thread (New Runnable () { @Override public void run () {// if the content of TextView is updated directly, the following asynchronous message processing mechanism is used// text.settext ("Nice to see you,bug"); message message = new message (); message.what = update_text; &Nbsp; handler.sendmessage (message); } }). Start (); break; default: break; } } }); }}
Here we first define an integral type constant Update_text, which is used to indicate an update TextView this 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 tobe equal to Update_text, the contents of the TextView display are changed to "nice" and "see" Again .
Here's another look .Change TextThe code in the Click event of the button. As you can see, this time we're not directly thread the Strand.Uioperation, instead of creating aMessage(Android.os.Message) object and set itWhatthe value of the field is specified asUpdate_text, and then callHandlerof theSendMessage ()method adds thisMessagesent out. Soon,Handleryou'll get this one .Message, and inHandlemessage ()method is used to process it. Note at this pointHandlemessage ()The code in the method is run in the main thread, so we can safely do it hereUioperation. Next toMessagecarry theWhatthe value of the field is judged if it is equal toUpdate_text, you willTextViewthe displayed content is changed toAgain.
used in APP Online Automation test tool:www.ineice.com
Updating the UI thread with asynchronous message processing