Method 1: Activity. runOnUiThread (Runnable)
(Experience: This is the best practice. Wherever you want to refresh the page,
Activity. runOnUiThread (new Runnable ()
{Public void run () {update UI }}
);
Method 2: The subthread calls Handler's sendMessage (message) to send events.
Copy codeThe Code is as follows: mHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
// Operation interface
MyText. setText (information from the network );
Super. handleMessage (msg );
}
};
Public class MyThread extends Thread {
Public void run (){
// Time-consuming operation
LoadNetWork ();
Message msg = new Message ();
MHandler. sendMessage (msg); // send a message to Handler,
}
}
Method 3: View. post (Runnable)
Copy codeThe Code is as follows: public void onClick (View v ){
New Thread (new Runnable (){
Public void run (){
// Time-consuming operation
LoadNetWork ();
MyText. (new Runnable (){
MyText. setText (information from the network );
});
}
}). Start ();
}
Method 4: AsyncTask
Copy codeThe Code is as follows: // executed in the UI thread
New downloadimagetask(cmd.exe cute ("www.91dota.com ");
Private class DownloadImageTask extends AsyncTask {
Protected String doInBackground (String... url ){
Return loadDataFormNetwork (url [0]); // time-consuming operations in the background
}
Protected void onPostExecute (String result ){
MyText. setText (result); // obtain information from the network to refresh the page
}
}