How to check if running on UI thread in Android?
android, multithreading
Solution
Doesn't look like there is a method for that in the SDK. The check is in the `ViewRoot` class and is done by comparing `Thread.currentThread()` to a class member which is assigned in the constructor but never exposed.
If you really need this check you have several options to implement it:
- catch the android.view.ViewRoot$CalledFromWrongThreadException
- `post` a `Runnable` to a view and check `Thread.currentThread()`
- use a `Handler` to do the same
In general I think instead of checking whether you're on the correct thread, you should just make sure the code is always executed on the UI thread (using 2. or 3.).
Problem
How can I know if the running code is executed on the main thread (UI thread)? With Swing I use the `isEventDispatchThread` method...