How does Android update the UI thread in other threads

Source: Internet
Author: User

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

}
}

Related Article

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.