How to know if this thread is a UI Thread

前端 未结 4 962
走了就别回头了
走了就别回头了 2021-02-05 06:28

Is there any way on Android to know, if the thread running my code, is the UI Thread or not ? In swing there was SwingUtilities.isEventDispatchThread() to tell me

4条回答
  •  野的像风
    2021-02-05 06:49

    Yes, there is a way. Check the current thread object against main lopper's thread object. Main looper is always in the UI thread.

    boolean isOnUiThread = Thread.currentThread() == Looper.getMainLooper().getThread();
    

提交回复
热议问题