In my Android application, I extract the code to update the user interface elements into a separate utility package for reuse. I would like my code to be active and update the user interface differently if the current execution context is from a user interface thread versus a non-UI thread.
Is it possible to programmatically determine whether the current execution is in progress on a user interface thread or not?
A trivial example of what I'm looking for is my application, which updates a lot TextViewall the time. So, I would like to have a static utility like this:
public static void setTextOnTextView(TextView tv, CharSequence text){
tv.setText(text);
}
This will obviously not work if called from a thread other than the UI. In this case, I would like to force the client code to pass in Handler, and also send the UI operation to the handler.
source
share