我需要检查运行某段代码的线程是否是主(UI)线程。我怎样才能做到这一点呢?


当前回答

只需记录这一行,它应该打印“main”。

Thread.currentThread () . name

其他回答

只需记录这一行,它应该打印“main”。

Thread.currentThread () . name

你可以在android DDMS logcat中验证它,其中进程id将是相同的,但线程id将不同。

简单的Toast消息也可以作为快速检查。

您可以尝试Thread.currentThread().isDaemon()

你可以检查一下

if(Looper.myLooper() == Looper.getMainLooper()) {
   // You are on mainThread 
}else{
// you are on non-ui thread
}