今天看到了一個runOnUiThread()方法用來更新UI,以爲很神奇!!html
方法一:handler機制不說了。java
FusionField.currentActivity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(getApplicationContext(), , "Update My UI", Toast.LENGTH_LONG).show(); } });
Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.android
若是當前線程是UI主線程,則直接執行Runnable的代碼;不然將Runnable post到UI線程的消息隊列。 --看代碼實現就知道了api
action the action to run on the UI threadide
public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action);//將Runnable Post到消息隊列,由內部的mHandler來處理,實際上也是Handler的處理方式 } else { action.run();//已經在UI線程,直接運行。 } }
參考地址:post