run In Main Thread Waiting - Android android.os

Android examples for android.os:Handler

Description

run In Main Thread Waiting

Demo Code


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

public class Main {
    static private final int defaultTimeoutMs = 2000;

    static public void runInMainThreadWaiting(Runnable runnable)
            throws InterruptedException {
        runInMainThreadWaiting(runnable, defaultTimeoutMs);
    }//w  w  w  .  ja v a  2 s  .c om

    static public void runInMainThreadWaiting(Runnable runnable,
            int timeoutMs) throws InterruptedException {

        Handler mainHandler = new Handler(Looper.getMainLooper());
        mainHandler.post(runnable);
        synchronized (runnable) {
            runnable.wait(timeoutMs);
        }
    }
}

Related Tutorials