/** * Run the supplied Runnable on the main thread. The method will block only if the current thread * is the main thread. * * @param r The Runnable to run */ public static void runOnUiThread(Runnable r) { if (runningOnUiThread()) { r.run(); } else { getUiThreadHandler().post(r); } }
/** * Run the supplied Runnable on the main thread. The method will block until the Runnable * completes. * * @param r The Runnable to run. */ public static void runOnUiThreadBlocking(final Runnable r) { if (runningOnUiThread()) { r.run(); } else { FutureTask<Void> task = new FutureTask<Void>(r, null); postOnUiThread(task); try { task.get(); } catch (Exception e) { throw new RuntimeException("Exception occured while waiting for runnable", e); } } }