一个定时循环任务的工具,可以用于执行每隔固定时间就要执行一次的任务。例如大图轮播,状态监测什么的。
共有4个Java文件。具体的使用方法在最下面。
Counter.java
package com.***.android.time; import java.util.HashMap; /** * Created by zpybless on 2015/11/18. */ public class Counter{ static class CountableState { int count; } //单例模式 private static Counter counter; public static Counter getInstance() { if (counter == null) { counter = new Counter(); } return counter; } private HashMap<ICountable, CountableState> states = new HashMap<ICountable, CountableState>(); //为每一个任务(使用同一个计时器)添加计数器,并启动任务instance.onCountIncreasesToOne(); //同一个TickTimer,调用多次start()的情况,计数器会增加 public void increase(ICountable instance) { CountableState state = states.get(instance); if (state == null) { state = new CountableState(); state.count = 1; states.put(instance, state); instance.onCountIncreasesToOne(); } else { state.count++; } } //结束任务,计数器-1,如果是最后一个线程,就结束instance.onCountDecreasesToZero();; public void decrease(ICountable instance) { CountableState state = states.get(instance); state.count--; if (state.count == 0) { states.remove(instance); instance.onCountDecreasesToZero(); } } public int getCount(ICountable instance) { CountableState state = states.get(instance); return state == null ? 0 : state.count; } }
ICountable.java
package com.***.android.time; /** * Created by zpybless on 2015/11/18. */ public interface ICountable { void onCountIncreasesToOne(); void onCountDecreasesToZero(); }
TickTimer.java
package com.***.android.time; import android.os.Handler; /** * 计时器 * Created by zpybless on 2015/11/18. */ public class TickTimer implements ICountable { /** * 计时器对外接口,每隔一段时间,执行某一任务 * @param duration 时间间隔 * @param runnable 任务进程 */ public TickTimer(TimeSpan duration, Runnable runnable) { this.duration = duration; this.runnable = runnable; } private static int nextSessionID; private int sessionID; private TimeSpan duration; private boolean isRunning; public boolean isRunning() { return isRunning; } private Runnable runnable; private static Handler handler = new Handler(); private Runnable onTickRunnable; //同一个TickTimer对象,对此调用此方法,onCountIncreasesToOne()只会被执行1次(第一次) public void start() { Counter.getInstance().increase(this); } public void stop() { Counter.getInstance().decrease(this); } private void onTick() { if (runnable != null) { runnable.run(); } } @Override public void onCountIncreasesToOne() { sessionID = nextSessionID; nextSessionID++; onTickRunnable = new Runnable() { public int sessionID = TickTimer.this.sessionID; public void run() { onTick(); if (isRunning && sessionID == TickTimer.this.sessionID) { //再次执行本线程,使onTick()里面的线程任务达到循环执行的效果 handler.postDelayed(onTickRunnable, duration.getMilliseconds()); } } }; //线程开关 isRunning = true; //启动线程 onTickRunnable.run(); } @Override public void onCountDecreasesToZero() { isRunning = false; } }
TimeSpan.java
package com.***.android.time; /**时间相关工具类 * Created by zpybless on 2015/11/18. */ public class TimeSpan { private long milliseconds; public long getMilliseconds() { return milliseconds; } public TimeSpan(long milliseconds) { this.milliseconds = milliseconds; } public int getMillisecondPart() { return (int)(milliseconds % 1000); } public int getSecondPart() { return getSecond(milliseconds); } public int getMinutePart() { return getMinute(milliseconds); } public int getHourPart() { return getHour(milliseconds); } @Override public String toString() { return getClockHmsString(milliseconds); } public static String getClockHmsString(long milliseconds) { return String.format("%d:%02d:%02d", getHour(milliseconds), getMinute(milliseconds), getSecond(milliseconds)); } public static String getCounterHmsString(long milliseconds) { return String.format("%02d:%02d:%02d", getHour(milliseconds), getMinute(milliseconds), getSecond(milliseconds)); } private static int getHour(long milliseconds) { return (int)(Math.floor((float)milliseconds / (1000 * 60 * 60))); } private static int getMinute(long milliseconds) { return (int)(Math.floor((float)milliseconds / (1000 * 60))) % 60; } private static int getSecond(long milliseconds) { return (int)(Math.floor((float)milliseconds / 1000)) % 60; } public static TimeSpan fromMilliseconds(float time) { return new TimeSpan((long)time); } public static TimeSpan fromSeconds(float time) { return new TimeSpan((long)(time * 1000)); } public static TimeSpan fromMinutes(float time) { return new TimeSpan((long)(time * 1000 * 60)); } public static TimeSpan fromHours(float time) { return new TimeSpan((long)(time * 1000 * 60 * 60)); } public static TimeSpan fromDays(float time) { return new TimeSpan((long)(time * 1000 * 60 * 60 * 24)); } }
具体使用:
TickTimer timer = new TickTimer(TimeSpan.fromSeconds(10.00f)【时间间隔】, new Runnable() {@Override public void run() { //要执行的任务 }});timer.start();

本文介绍了一个用于定时循环执行任务的工具,适用于大图轮播、状态监测等场景。主要包括Counter、TickTimer和TimeSpan三个Java文件,详细使用方法在文章底部给出。
1030

被折叠的 条评论
为什么被折叠?



