Android updates the UI thread in other threads

Source: Internet
Author: User

Http://www.cnblogs.com/shangdahao/archive/2013/05/19/3069092.html

 

Android updates the UI thread in other threads

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.

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)

Public void onclick (view v) {New thread (New runnable () {public void run () {// loadnetwork (); mytext. (new runnable () {mytext. settext (information from the network );});}}). start ();}

Method 4: asynctask

// Execute new downloadimagetask(cmd.exe cute ("www.91dota.com") in the UI thread; private class downloadimagetask extends asynctask {protected string doinbackground (string... URL) {return loaddataformnetwork (URL [0]); // time-consuming operation in the background} protected void onpostexecute (string result) {mytext. settext (result); // get the 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.