{"id":1885,"date":"2014-11-13T11:57:46","date_gmt":"2014-11-13T06:27:46","guid":{"rendered":"http:\/\/codetheory.in\/?p=1885"},"modified":"2014-11-13T11:59:50","modified_gmt":"2014-11-13T06:29:50","slug":"checkdetect-whether-current-thread-uimain-thread-android","status":"publish","type":"post","link":"https:\/\/codetheory.in\/checkdetect-whether-current-thread-uimain-thread-android\/","title":{"rendered":"How to Check\/Detect Whether Current Thread is UI\/Main Thread or Not in Android"},"content":{"rendered":"
Checking whether the current thread is the UI\/Main thread or some other background thread is very easy. Here’s the code for it:<\/p>\n
<\/p>\n
\r\nif (Looper.myLooper() == Looper.getMainLooper()) {\r\n \/\/ Current thread is the UI\/Main thread\r\n} \r\n\r\n\/\/ Another approach\r\n\r\nif (Looper.getMainLooper().getThread() == Thread.currentThread()) {\r\n \/\/ Current thread is the UI\/Main thread\r\n}\r\n<\/pre>\n