- /**
- * 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();
- }
- }
使用:
runOnUiThread可以帮助你在线程中执行UI更新操作,我们只需要在线程中写上类似
youractivity. runOnUiThread(new Runnable() {
@Override
public void run() {
// refresh ui 的操作代码
}
});
这里需要注意的是runOnUiThread是Activity中的方法,在线程中我们需要告诉系统是哪个activity调用,所以前面显示的指明了activity。