post Background Thread And Ui Thread - Android java.lang

Android examples for java.lang:Thread

Description

post Background Thread And Ui Thread

Demo Code


//package com.java2s;
import android.os.Handler;
import android.os.Looper;

public class Main {
    private static Handler sHandler;

    public static void postBackgroundAndUi(final Runnable background1,
            final Runnable ui2) {
        postBackground(new Runnable() {
            @Override/*from   w w  w.  j a  v  a2  s  .c  o m*/
            public void run() {
                if (background1 != null)
                    background1.run();
                if (ui2 != null)
                    post(ui2);
            }
        });
    }

    public static void postBackground(final Runnable run) {
        postBackgroundDelayed(run, 0);
    }

    public static void post(Runnable run) {
        postDelayed(run, 0);
    }

    public static void postBackgroundDelayed(final Runnable run,
            long delayMills) {
        if (run == null)
            return;
        getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Thread thread = new Thread(run);
                thread.start();
            }
        }, delayMills);
    }

    public static void postDelayed(Runnable run, long delayMills) {
        if (run == null)
            return;
        getHandler().postDelayed(run, delayMills);
    }

    public static Handler getHandler() {
        if (sHandler == null)
            sHandler = new Handler(Looper.getMainLooper());
        return sHandler;
    }
}

Related Tutorials