1) using the Activity.runonuithread (runable action) method
Scenario One:
In the main thread, define the method and start the thread in the method.
Public classMainactivityextendsActivity {//... Private voidMyTask () {Thread Mthread=NewThread () {//New Thread Public voidrun () {Sleep (1000); //Update UIRunonuithread (NewRunnable () {@Override Public voidrun () {Mtextview.text= "123"; } }); }; }; Mthread.start (); }}
Scenario Two:
Thread operations are implemented through custom thread classes, and threads access the UI thread through a handle. (Less coupling)
Public classXxthreadextendsThread {//defining UI thread handlesHandler Uihandler =NewHandler (Looper.getmainlooper ()); //Thread main function Public voidrun () {//Update UIUihandler.post (NewRunnable () {@Override Public voidrun () {Mtextview.text= "123"; } }); }}
2) using the message mechanism
Ways to update the UI in Android