Run a task on a non-UI thread after the delay - Android java.lang

Android examples for java.lang:Thread

Description

Run a task on a non-UI thread after the delay

Demo Code

/**//from w  w  w. j  a v a2 s. c  o  m
 * Various utilities functions for NCA
 * 
 * @author sonal.agarwal
 * 
 * @Copyright (c) 2014 Nymi Inc. All rights reserved.
 */
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Vibrator;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class Main{
    protected static ScheduledThreadPoolExecutor executor;
    /**
     * Run a task on a non-UI thread after the delay
     * 
     * @param runnable
     *            the runnable
     * @param delay
     *            the delay to run the task, it is in milliseconds
     * @return a ScheduledFuture to managing the task
     */
    public static ScheduledTask runTaskAfterMillies(Runnable runnable,
            long delay) {
        if (executor == null) {
            synchronized (SystemUtils.class) {
                if (executor == null) {
                    executor = new ScheduledThreadPoolExecutor(2);
                }
            }
        }

        if (delay > 0) {
            return new ScheduledTask(executor.schedule(runnable, delay,
                    TimeUnit.MILLISECONDS), runnable);
        } else {
            executor.execute(runnable);
            return null;
        }
    }
}

Related Tutorials