首页 > 技术文章 > Android判断当前线程是否是主线程的方法

error404 2014-07-23 18:14 原文

开发过程中有时候会在Thread类中执行某些操作,有些操作会由于Android版本的不同,尤其是低版本而Crash,因此必要的时候会查看某些容易引起crash的操作是否是在主线程,这里举三种方法:

方法一:使用Looper类判断

Looper.myLooper() == Looper.getMainLooper()

方法二:通过查看Thread类的当前线程

Thread.currentThread() == Looper.getMainLooper().getThread()

推荐阅读