/** * 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. * * @param action the action to run on the UI thread */ public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); } }
在非ui thread中想要更新ui操作,需要使用runOnUiThread来完成。若当前线程是ui thread则立即执行,否则action 会被post到ui thread的event queue中。