[英]How to set alarm manager using date and time from Textview
我正在将timestamp
转换为date and time
并在textview
上设置结果。
例如1443884578
是Sat 3 October 2015 18:02
我想将上述date and time
为alarm manager
。研究后,我发现了一个使用日期时间选择器的代码。
public void onDateSelectedButtonClick(View v) { // Get the date from our datepicker int day = picker.getDayOfMonth(); int month = picker.getMonth(); int year = picker.getYear(); // Create a new calendar set to the date chosen // we set the time to midnight (ie the first minute of that day) Calendar c = Calendar.getInstance(); c.set(year, month, day); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service scheduleClient.setAlarmForNotification(c); // Notify the user what they just did Toast.makeText(this, "Notification set for: " + day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show(); }
但是,它仅获取date
并在日期发生的那一刻触发alarm
。
问题:我想从textview
获取date
和time
,并以我的格式跳过此日期时间选择器。 这可能吗?
String input = "Sat October 3 2015 18:02"; // Instead of String input = "Mon Feb 06 2015";
Calendar cal = Calendar.getInstance();
Date date = new Date();
// Changed the format to represent time of day
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
try {
date = sdf.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(date);
//We haven't parsed the seconds from the original date so this will result
//in 18:02:00 - 10seconds.
//For a correct calculation, you could parse the seconds as well
//See SimpleDateFormat above, but you would have to provide the original date
//with seconds as well
cal.add(Calendar.SECOND, -10);
scheduleClient.setAlarmForNotification(cal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.