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