I am trying to post a toast after calling a function from a non UI thread in a widget. I\'ve read multiple ways of doing this (post/new handler/broadcast) but most methods s
You can create your own version of runOnUiThread(). This is what I use when I need to run something in the UI thread from outside an Activity:
public final class ThreadPool {
private static Handler sUiThreadHandler;
private ThreadPool() {
}
/**
* Run the {@code Runnable} on the UI main thread.
*
* @param runnable the runnable
*/
public static void runOnUiThread(Runnable runnable) {
if (sUiThreadHandler == null) {
sUiThreadHandler = new Handler(Looper.getMainLooper());
}
sUiThreadHandler.post(runnable);
}
// Other, unrelated methods...
}
Then, you can simply call ThreadPool.runOnUiThread(runnable).
You can find more information on how this works in this post series: Android: Looper, Handler, HandlerThread. Part I