Old problem: 3 ways to update the UI in an Android sub-thread

Source: Internet
Author: User

There are often problems with Android projects that update the UI after a time-consuming operation in a child thread, and then summarize the updated methods for some of the projects that you have experienced:

Method One: With handler

1, the main thread defined handler:

Handler Mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); switch ( Msg.what) {Case 0://completes the main interface update, gets the data String "= (string) msg.obj;updateweather (); Textview.settext (data); Break;default: break;}};

2, the child thread sends the message, notifies handler to complete the UI update:

private void Updateweather () {New Thread (new Runnable () {@Overridepublic void Run () {//time-consuming operation, sending a message to handler after completion, completing UI update ; mhandler.sendemptymessage (0);//requires data transfer, using the following method; message msg =new message (); msg.obj = "data";//Can be a basic type, can be an object, can be a list, map, etc. ; Mhandler.sendmessage (msg);}}). Start ();}

The handler object of method one must be defined in the main thread, if multiple classes are called directly to each other, it is not very convenient to pass the Content object or call through the interface;

Method Two: Update with Runonuithread

Update the UI with the Runonuithread () method in a child thread:

New Thread () {public void run () {///Here is a time-consuming operation, update Ui;runonuithread (new Runnable () {@Overridepublic void Run () {// Update Uiimageview.setimagebitmap (bitmap);}});}. Start ();

If it is in a non-contextual class (Activity), it can be invoked by passing the context;

Activity activity = (activity) imageview.getcontext (); Activity.runonuithread (new Runnable () {@Overridepublic void run ( ) {Imageview.setimagebitmap (bitmap);}});

This method is more flexible to use, but if the thread is defined elsewhere, the activity object needs to be passed;

Method Three: View.post (Runnable R)

Imageview.post (New Runnable () {@Overridepublic void run () {imageview.setimagebitmap (bitmap);}});

This method is simpler, but it needs to pass the view to be updated;

Old problem: 3 ways to update the UI in an Android sub-thread

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.