工具类

1. 运行在主线程

public class ThreadUtils {/**
     * Run a runnable on the Main (UI) Thread.
     * @param runnable the runnable
     */
    public static void runOnUiThread(final Runnable runnable) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            new Handler(Looper.getMainLooper()).post(runnable);
        } else {
            runnable.run();
        }
    }

/**
* Returns {@code true} if called on the main thread, {@code false} otherwise.
*/
public static boolean isOnMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}

}

参考: Android ThreadUtil 线程公共类,判断是否在主线程/ 子线程执行 相关操作

 

posted @ 2017-10-14 12:20  huyang011  阅读(113)  评论(0编辑  收藏  举报