android 检测当前是否是Main线程的两种方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dhl_1986/article/details/82348679

开发过程中,我们需要知道当前的线程是否为UI线程(主线程),在这里有两个简单的方法判断,但是本质上是一样的

1,根据Lopper.myLooper 判断:

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

2,根据Looper 获取的Thread 判断:

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

这两种方法本质是一样的。

猜你喜欢

转载自blog.csdn.net/dhl_1986/article/details/82348679