使用Activity类中的公共方法在UI线程中进行耗时操作

66 阅读1分钟

查看Activity.class中源码

/**
     * 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();
        }
    }

只要继承Activity就能使用这个方法

在UI线程上运行指定的行动。如果UI线程是当前线程,然后立即执行行动。如果当前线程不是UI线程,耗时操作则是发布到UI线程的事件队列。
使用例子请看blog.csdn.net/yang7866542…,在UI线程中更改进度条。