在近期的项目中有个需求就是在用户选择了代抢位的时间段没有抢到车位就给用户一个友好的提示,比方用户选择的是
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还有非常多重载的方法
如图:
在此记录下