放上一个指定详细日期及时间的timer
1 public class MainActivity extends Activity { 2 3 private Handler handler = new Handler(){ 4 @Override 5 public void handleMessage(Message msg) { 6 switch (msg.what){ 7 case 1: 8 Log.i("====","执行了"); 9 //需要执行的代码放这里 10 break; 11 } 12 } 13 }; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 TimerTask task = new TimerTask() { 21 @Override 22 public void run() { 23 handler.sendEmptyMessage(1); 24 } 25 }; 26 Timer timer = new Timer(true); 27 timer.schedule(task,strToDateLong("2016-06-14 15:33:30")); 28 } 29 30 31 /** 32 * string类型时间转换为date 33 * @param strDate 34 * @return 35 */ 36 public static Date strToDateLong(String strDate) { 37 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 38 ParsePosition pos = new ParsePosition(0); 39 Date strtodate = formatter.parse(strDate, pos); 40 return strtodate; 41 } 42 }
时间: 2024-10-05 05:01:47