You basically just add one and send either a Message
or a Runnable
.
private final Handler mUiThreadHandler = new Handler(Looper.getMainLooper());
void fromAnyThread() {
final String importantDataFromBackGroundThread = "!!!";
mUiThreadHandler.post(new Runnable() {
@Override
public void run() {
System.out.println("Hi from Ui Thread:" + importantDataFromBackGroundThread);
}
});
}
The Handler
handles all messages / runnables within the Thread it runs in. If you specify Looper.getMainLooper()
you are guaranteed that this is the main thread.