run On Ui Thread - Android java.lang

Android examples for java.lang:Thread

Description

run On Ui Thread

Demo Code


import android.os.Handler;
import android.os.Looper;

public class Main{
    public static void runOnUiThread(Runnable runnable) {
        if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
            runnable.run();/*from   ww  w.  j a va 2 s .c  om*/
        } else {
            new Handler(Looper.getMainLooper()).post(runnable);
        }
    }
    public static SerializeCallback runOnUiThread(
            final SerializeCallback callback) {
        return new SerializeCallback() {
            @Override
            public void onComplete(final Object data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onComplete(data);
                    }
                });
            }

            @Override
            public void onFailed() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        callback.onFailed();
                    }
                });
            }
        };
    }
}

Related Tutorials