Use of the Android Activity Runonuithread () method

Source: Internet
Author: User

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).

Runnable can be called in the UI program.

 /**   * 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. * *   @param   action the action to run on the UI thr EAD  */ public  final  void   Runonuithread (Runnable action) { if  (    Thread.CurrentThread ()!= Muithread) {mhandler.post (action);     else   {Action.run (); }}

As can be seen from the source code above, the program will first determine whether the current thread is the UI thread, if it is directly run, if not the post, then its essence is to use the handler mechanism to handle the thread and UI communication.

private ProgressDialog ProgressDialog;
Context Mcontext;

ProgressDialog =NewProgressDialog (Mcontext); String Stri=mcontext.getresources (). getString (R.string.is_sending_a_request); Progressdialog.setmessage (Stri); Progressdialog.setcanceledontouchoutside (false); Progressdialog.show (); NewThread (NewRunnable () { Public voidrun () {Try {( Activity) mcontext). Runonuithread (NewRunnable () { Public voidrun () {Progressdialog.dismiss (); String S1=mcontext.getresources (). getString (r.string.send_successful); Toast.maketext (Mcontext, S1, Toast.length_long). Show (); } }); } Catch(FinalException e) {(Activity) mcontext). Runonuithread (NewRunnable () { Public voidrun () {Progressdialog.dismiss (); String S2=mcontext.getresources (). getString (R.string.request_add_buddy_failure); Toast.maketext (mcontext, S2+e.getmessage (), Toast.length_long). Show (); } }); }}). Start ();

Creating ProgressDialog in this way is convenient, or refreshing adapter is easier than using Thread+handler.

If it is not created in Activity, it needs to be preceded by (activity) mcontext) .

Use of the Android Activity Runonuithread () method

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.