How can I know if the running code is executed in the main thread (UI thread). With Swing I use the isEventDispatchThread method...
+2
A:
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
aRunnable
to a view and checkThread.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.).
Josef
2009-12-04 09:34:33
+1 for (3) < padding for the 15 limit >
Will
2009-12-04 09:39:57
+1
A:
You may also use runOnUiThread, it only requires a runnable that will be run in the ui thread
michael
2010-07-22 08:39:20