Kart:Android:Timer

From FSI
Jump to: navigation, search
package ch.hevs.utils.timer;
 
import android.os.Handler;
 
/**
 * Schedules one-shot or recurring tasks for execution.
 *
 * You have to extend this abstract class and implement the method onTimeout().
 *
 * The timer runs inside the Android main UI, so there will be no problems when using UI elements
 * from the timer's onTimeout() method. If the UI main thread is busy, timer execution may be
 * subject to delays.
 *
 * One-shot timers are scheduled to run after a relative delay.
 *
 * Periodically timers are scheduled with a fixed period and the very first time they are executed
 * immediately.
 *
 * This class does not offer guarantees about the real-time nature of task scheduling.
 *
 * @author Michael Clausen (clm@hevs.ch)
 * @version 2.2 - 2015
 */
public abstract class Timer {
    /**
     * This method will be called either once after the specified delay or periodically whenever
     * the timer expires.
     */
    public abstract void onTimeout();
 
    /**
     * Schedules the timer once to expire after the given delay.
     *
     * Note that if the timer has already scheduled, this method will do nothing at
     *
     * @param delayMs   The delay in milliseconds.
     */
    public void scheduleOnce(final int delayMs) {
        if (interval == 0 && delayMs > 0) {
            continuously = false;
            interval = delayMs;
            handler.postDelayed(runnable, delayMs);
        }
    }
 
    /**
     * Schedules the timer continuously using the given interval. Note that the first time the timer
     * expires immediately.
     *
     * @param intervalMs    Interval between the points in time the timer expires.
     */
    public void schedulePeriodically(final int intervalMs) {
        if (interval == 0 && intervalMs > 0) {
            continuously = true;
            interval = intervalMs;
            handler.post(runnable);
        }
    }
 
    /**
     * Stops/cancels/pauses the pending timer. If the timer was scheduled once and did not already
     * expire, it will canceled. Periodically timers will be paused.
     *
     * You can reschedule timers, but you need to wait until the ready() method returns true.
     */
    public void stop() {
        interval = -1;
    }
 
    /**
     * If this method returns true, it is save to schedule the timer.
     *
     * @return  True if the timer is ready to be rescheduled.
     */
    public boolean ready() {
        return interval == 0;
    }
 
    private boolean continuously = false;
    private int interval = 0;
    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (interval > 0) {
                if (continuously) {
                    handler.postDelayed(runnable, interval);
                }
                onTimeout();
            }
            if (interval == -1) {
                interval = 0;
            }
        }
    };
}
Personal tools
Namespaces
Variants
Actions
Navigation
Modules / Projects
Browse
Toolbox