public class MainActivity extends Activity { private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case :
Log.i("print","执行了");
//需要执行的代码放这里
break;
}
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TimerTask task = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage();
}
};
Timer timer = new Timer(true);
timer.schedule(task,strToDateLong("2017-01-09 11:32:00"));
} /**
* string类型时间转换为date
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition();
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
}
转自http://blog.csdn.net/Corey_Jia/article/details/51672274