Android examples for java.lang:Thread
Always post action to the UI Thread for execution
//package com.java2s; import android.os.Handler; import android.os.Looper; public class Main { private static Handler uiHandler; /**/*from www .j a v a 2 s. co m*/ * Same as calling #postOnUiThread(action,false) */ public static void postOnUiThread(final Runnable action) { postOnUiThread(action, false); } /** * Always post action to the UI Thread for execution * * @param action procedure to run on the UI Thread * @param wait true if we should wait for action to complete before returning to calling process */ public static void postOnUiThread(final Runnable action, final boolean wait) { if (uiHandler == null) { uiHandler = new Handler(Looper.getMainLooper()); } uiHandler.post(new Runnable() { @Override public void run() { synchronized (action) { action.run(); if (wait) { action.notify(); } } } }); waitForAction(action, wait); } protected static void waitForAction(final Runnable action, boolean wait) { if (wait) { synchronized (action) { try { action.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }