简体   繁体   English

如何使用Textview中的日期和时间设置警报管理器

[英]How to set alarm manager using date and time from Textview

I am converting a timestamp into date and time and setting the result on a textview . 我正在将timestamp转换为date and time并在textview上设置结果。

For example 1443884578 is Sat 3 October 2015 18:02 例如1443884578Sat 3 October 2015 18:02

I would like to set the above date and time into an alarm manager .After research i found a code that uses a date time picker. 我想将上述date and timealarm 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(); } 

However its getting only the date and fires the alarm the minute the date occurs. 但是,它仅获取date并在日期发生的那一刻触发alarm

PROBLEM: I would like to get the date and time from my textview and skip this date time picker in the format i have. 问题:我想从textview获取datetime ,并以我的格式跳过此日期时间选择器。 Is this possible? 这可能吗?

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM