Skip to content

判断当前线程是否主线程?

安卓小煜 edited this page Oct 14, 2019 · 1 revision

方法一:

public boolean isMainThread() {
    return Looper.getMainLooper() == Looper.myLooper();
}

方法二:

public boolean isMainThread() {
    return Looper.getMainLooper().getThread() == Thread.currentThread();
}

方法三:

public boolean isMainThread() {
    return Looper.getMainLooper().getThread().getId() == Thread.currentThread().getId();
}

参考

Clone this wiki locally