1. /**
2. * Runs the specified action on the UI thread. If the current thread is the UI
3. * thread, then the action is executed immediately. If the current thread is
4. * not the UI thread, the action is posted to the event queue of the UI thread.
5. *
6. * @param action the action to run on the UI thread
7. */
8. public final void runOnUiThread(Runnable action) {
9. if (Thread.currentThread() != mUiThread) {
10. mHandler.post(action);
11. else {
12. action.run();
13. }
14. }
使用:
runOnUiThread可以帮助你在线程中执行UI更新操作,我们只需要在线程中写上类似
youractivity. runOnUiThread(new Runnable() {
@Override
public void run() { // refresh ui 的操作代码
}
});
这里需要注意的是runOnUiThread是Activity中的方法,在线程中我们需要告诉系统是哪个activity调用,所以前面显示的指明了activity。