Java tutorial
//package com.java2s; import android.os.Handler; import android.os.Looper; public class Main { public static Handler mMainHandler; public static void runInMainThread(Runnable runnable) { if (isInMainThread()) { runnable.run(); } else { post(runnable); } } public static boolean isInMainThread() { return Looper.myLooper() == Looper.getMainLooper(); } public static boolean post(Runnable runnable) { return getMainThreadHandler().post(runnable); } public static Handler getMainThreadHandler() { if (mMainHandler == null) { mMainHandler = new Handler(Looper.getMainLooper()); } return mMainHandler; } }