如何检查是否当前线程不是主线程线程、主线、不是

2023-09-13 23:38:58 作者:半夏微凉。

我需要检查,如果运行的某一块code线程的主(UI)线程上运行与否。我怎样才能做到这一点?

解决方案

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

如果返回true,那么你是在UI线程!

I need to check if the thread running a certain piece of code is running on the main (UI) thread or not. How can I achieve this?

解决方案 线程概念 线程控制

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

if this returns true, then you're on the UI thread!