Two ways to update the UI for Android--handler and Runonuithread ()

Source: Internet
Author: User

Today I saw a Runonuithread () method used to update the UI, it feels amazing!!

Method One: The handler mechanism does not say.

Method Two: Use Activity.runonuithread (Runnable) to create the code that updates the UI in Runnable, and then pass the Runnable object to Activity.runonuithread when the UI needs to be updated ( Runnable). This way, the runnable can be called in the UI program. If the current thread is the UI thread, then the action is performed immediately. If the current thread is not the UI thread, the action is the UI thread that publishes to the event queue
FusionField.currentActivity.runOnUiThread (new  Runnable ()            {                publicvoid  Run ()                {                    "Update My UI",                            Toast.length_long). Show ();                }                });  
Public final void Runonuithread (Runnable action) Added in API Level 1

Runs the specified action on the UI thread. If the current thread was the UI thread, then the action is executed immediately. If the current thread was not the UI thread, the action was posted to the event queue of the UI thread.

If the current thread is the main UI thread, execute the runnable code directly, otherwise runnable post to the message queue of the UI threads. -- Just look at the code implementation.

Parameters

Action the action to run on the UI thread

 Public Final void Runonuithread (Runnable action) {         if (Thread.CurrentThread ()! = Muithread) {             Mhandler.post (action); // The runnable post to the message queue is handled by the internal Mhandler, which is actually the handler process  .       Else {             action.run (); // is already running directly on the UI thread.          }     }  

Reference Address:

Http://www.android100.org/html/201406/07/20384.html

http://blog.csdn.net/annkie/article/details/8496219

Two ways to update the UI for Android--handler and Runonuithread ()

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.