在最近的项目中有个需求就是在用户选择了代抢位的时间段没有抢到车位就给用户一个友好的提示,比如用户选择的是
2014-10-08:18:00这个时间,那么到了这个时间如果用户没有枪到车位就弹出一个对话框告诉用户,让用户做其他选择,
在android中一般有2中做法,一种是使用闹钟,也就是AlarManger,还有一种是Timer(定时器)
在此项目中就使用第二种方法实现
新建一个java项目 TimerDemo
public class TimerDemo {
public static void main(String[] args) {
Timer timer = new Timer();
DateTask task = new DateTask();
String current = "2014-10-8 11:39";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = null;
try {
date = simpleDateFormat.parse(current);
} catch (ParseException e) {
e.printStackTrace();
}
timer.schedule(task, date);
}
static class DateTask extends TimerTask{
@Override
public void run() {
System.out.println("时间到了");
}
}
}
Timer还有很多重载的方法
如图:
在此记录下