定时器使我们写程序不可或缺的一个模块,通常一个程序不仅仅使用一个定时器,那我们创建多个定时器不仅仅影响美观,而且不方便,做很多无用功。所以本人在写程序前肯定提前导入封装好的定时器类,以便随时调用。
下面贴上代码:
package com.util.timer;
public final class BasicTimer {//对外接口
static private BasicTimerManager mManager = BasicTimerManager.getInstance();
public interface BasicTimerCallback{
void onTimer();
}
//注意:不可使用对象调用startOneshot()
public static void startOneshot(int millisec, BasicTimerCallback cb){
mManager.startOneshot(new BasicTimer(cb), millisec);
}
public BasicTimer(BasicTimerCallback cb){
mManager.addTimer(this, cb);
}
public void start(int millisec){
mManager.start(this, millisec);
}
public void stop(){
mManager.stop(this);
}
//调用cancel()后,定时器将不再可用
public void cancel(){
mManager.remove(this);
}
public boolean isRunning(){
return mManager.isRunning(this);
};
}
结构体:
package com.util.timer;
final class BasicTimerInfo {
int init; //初始计数值
int count; //间隔计数,即基于基础定时的倍率
boolean oneshot; //是否为单次定时
Object cb; //定时回调
}
package com.util.timer;
import android.os.Handler;
import android.os.Message;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
final class BasicTimerManager {
private static BasicTimerManager sInstance;
private Map<BasicTimer, BasicTimerInfo> mInfos = new ConcurrentHashMap<>();//必须使用并发类
private Timer mTimer = new Timer();
private static final int BASE_INTERAL_MILLISEC = 100;
private BasicTimerManager(){
mTimer.schedule(new TimerTask() {
@Override
public void run() {
doRun();
}
}, 0, BASE_INTERAL_MILLISEC);
}
public static BasicTimerManager getInstance(){
if (sInstance == null){
sInstance = new BasicTimerManager();
}
return sInstance;
}
public void addTimer(BasicTimer timer, BasicTimer.BasicTimerCallback cb){
BasicTimerInfo info = new BasicTimerInfo();
info.cb = cb;
info.init = Integer.MAX_VALUE;
info.count = Integer.MAX_VALUE;
mInfos.put(timer, info);
}
public void start(BasicTimer timer, int millisec){
startTimer(timer, millisec, false);
}
public void startOneshot(BasicTimer timer, int millisec){
startTimer(timer, millisec, true);
}
public void stop(BasicTimer timer){
if (mInfos.containsKey(timer)){
BasicTimerInfo info = mInfos.get(timer);
info.init = Integer.MAX_VALUE;
info.count = Integer.MAX_VALUE;
}
}
public void remove(BasicTimer timer){
if (mInfos.containsKey(timer)){
mInfos.remove(timer);
}
}
public boolean isRunning(BasicTimer timer){
boolean running = false;
if (mInfos.containsKey(timer)){
BasicTimerInfo info = mInfos.get(timer);
running = !(info.init == Integer.MAX_VALUE && info.count == Integer.MAX_VALUE);
}
return running;
}
private void startTimer(BasicTimer timer, int millisec, boolean oneshot){
if (mInfos.containsKey(timer)){
BasicTimerInfo info = mInfos.get(timer);
info.count = millisec / BASE_INTERAL_MILLISEC;
info.init = millisec / BASE_INTERAL_MILLISEC;
info.oneshot = oneshot;
//保证不能为0
info.count = Math.max(info.count, 1);
info.count = Math.max(info.init, 1);
}
}
private void doRun(){
List<Object> ls = new ArrayList<>();
for (Map.Entry entry: mInfos.entrySet()){
BasicTimerInfo info = (BasicTimerInfo)entry.getValue();
if(--info.count == 0) {
if (info.oneshot){
info.count = Integer.MAX_VALUE;
info.init = Integer.MAX_VALUE;
}else {
info.count = info.init;
}
//先保存所有到时的定时器,统一发送到主线程处理
ls.add(entry);
}
}
if (!ls.isEmpty()){
Message message = new Message();
message.what = 0;
message.obj = ls;
mHander.sendMessage(message);
}
}
private Handler mHander = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
List<Object> ls = (List<Object>) msg.obj;
for (Object o: ls){
Map.Entry entry = (Map.Entry)o;
BasicTimer timer = (BasicTimer)entry.getKey();
BasicTimerInfo info = (BasicTimerInfo)entry.getValue();
((BasicTimer.BasicTimerCallback)info.cb).onTimer();
if (info.oneshot){ //单次定时到时后,删除定时器对象
remove(timer);
}
}
return false;
}
});
}
应用方法:
单次定时器:
BasicTimer.startOneshot(1500, new BasicTimer.BasicTimerCallback() {
@Override
public void onTimer() {
shipLocation();
}
});
BasicTimer vesselBasicTimer = new BasicTimer(new BasicTimer.BasicTimerCallback() {
@Override
public void onTimer() {
//干你想干的事
}
});
vesselBasicTimer.start(5000);
源码下载地址:http://download.csdn.net/detail/m0_38082472/9876450
喜欢就请点赞;谢谢!