Android判断是否在主线程

时间:2022-12-16 18:31:36

一切都是为了更好的封装!


方法1:

if (Looper.myLooper() != Looper.getMainLooper()) {

方法2:获取当前线程或其属性 进行比较

private synchronized void refreshProgress(int id, int progress, boolean fromUser,
        boolean animate) {
    if (mUiThreadId == Thread.currentThread().getId()) {
        doRefreshProgress(id, progress, fromUser, true, animate);
    } else {
        if (mRefreshProgressRunnable == null) {
            mRefreshProgressRunnable = new RefreshProgressRunnable();
        }

        final RefreshData rd = RefreshData.obtain(id, progress, fromUser, animate);
        mRefreshData.add(rd);
        if (mAttached && !mRefreshIsPosted) {
            post(mRefreshProgressRunnable);
            mRefreshIsPosted = true;
        }
    }
}
这里是progress bar的代码,就是比较id