今天看到了一个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