Kart:Android:Timer

(Difference between revisions)
Jump to: navigation, search
(Created page with "{{TOC right}} <source lang="java"> package ch.hevs.utils.timer; import android.os.Handler; public abstract class Timer { public abstract void onTimeout(); public vo...")
 
Line 5: Line 5:
 
import android.os.Handler;
 
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.1 - 2015
 +
*/
 
public abstract class Timer {
 
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();
 
     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) {
 
     public void scheduleOnce(final int delayMs) {
         if (interval == 0) {
+
         if (interval == 0 && delayMs > 0) {
 
             continuously = false;
 
             continuously = false;
 
             interval = 0;
 
             interval = 0;
Line 16: Line 46:
 
     }
 
     }
  
 +
    /**
 +
    * 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) {
 
     public void schedulePeriodically(final int intervalMs) {
         if (interval == 0) {
+
         if (interval == 0 && intervalMs > 0) {
 
             continuously = true;
 
             continuously = true;
 
             interval = intervalMs;
 
             interval = intervalMs;
Line 24: Line 60:
 
     }
 
     }
  
 +
    /**
 +
    * 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() {
 
     public void stop() {
         interval = 0;
+
         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;
 
     }
 
     }
  
Line 34: Line 85:
 
         @Override
 
         @Override
 
         public void run() {
 
         public void run() {
             onTimeout();
+
             if (interval > 0) {
            if (continuously && interval != 0) {
+
                if (continuously) {
                handler.postDelayed(runnable, interval);
+
                    handler.postDelayed(runnable, interval);
 +
                }
 +
                onTimeout();
 +
            }
 +
            if (interval == -1) {
 +
                interval = 0;
 
             }
 
             }
 
         }
 
         }
 
     };
 
     };
 
}
 
}
 
 
</source>
 
</source>

Revision as of 10:25, 20 August 2015

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.1 - 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 = 0;
            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