Run Callback On Main Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
//execute code on main thread
}
});
As long as you have a Context, you can do something like this:
Handler mainHandler = new Handler(context.getMainLooper());
And to run code on UI thread:
mainHandler.post(new Runnable() {
@Override
public void run() {
// run code
}
});
As suggested by kaka:
You could also use the static Looper.getMainLooper() which
Returns the application's main looper, which lives in the main thread of the application.