AlarmManager无法在特定日期工作

时间:2012-05-03 19:40:01

标签: android service calendar alarmmanager android-pendingintent

我一直在尝试将AlarmManager设置为特定日期,但它根本不起作用!当我只是使用时间它工作正常。 这是代码:

SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(this);
            String getEventTime = prefs.getString("EventSTime", "");
            String getEventDate = prefs.getString("EventDate", "");
            Toast.makeText(this, getEventTime + " " + getEventDate,
                    Toast.LENGTH_LONG).show();
            String temp = "", temp2 = "";
            DateFormat dateformat = new SimpleDateFormat("HH:mm");
            DateFormat dateformatDate = new SimpleDateFormat("dd-MM-yyyy");
            try {
                temp = dateformat.format(dateformat.parse(getEventTime));
                temp2 = dateformatDate.format(dateformatDate
                        .parse(getEventDate));
                Toast.makeText(this, temp + " " + temp2, Toast.LENGTH_LONG)
                        .show();

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String getEventTimeSubStringHour = temp.substring(0, 2);
            String getEventTimeSubStringMinute = temp.substring(3, 5);
            String getEventDateSubStringDay = temp2.substring(0, 2);
            String getEventDateSubStringMonth = temp2.substring(3, 5);
            String getEventDateSubStringYear = temp2.substring(6, 10);
            Toast.makeText(
                    this,
                    getEventTimeSubStringHour + " "
                            + getEventTimeSubStringMinute + " "
                            + getEventDateSubStringDay + " "
                            + getEventDateSubStringMonth + " "
                            + getEventDateSubStringYear, Toast.LENGTH_LONG)
                    .show();
            hourOfDay = Integer.parseInt(getEventTimeSubStringHour);
            minute = Integer.parseInt(getEventTimeSubStringMinute);
            day = Integer.parseInt(getEventDateSubStringDay);
            month = Integer.parseInt(getEventDateSubStringMonth);
            year = Integer.parseInt(getEventDateSubStringYear);
            int second = 0;
//          calendar.setTimeInMillis(System.currentTimeMillis());
//          calendar.clear();
            //calendar.set(year, month, day);
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calendar.set(Calendar.MINUTE, minute);
            calendar.set(Calendar.SECOND, second);


            Intent intent = new Intent(MessageService.this,
                    ServiceManager.class);
            PendingIntent callToServiceManager = PendingIntent.getService(
                    MessageService.this, 2, intent, 0);
            AlarmManager alarmToCallServiceManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmToCallServiceManager.set(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), callToServiceManager);

即使使用calendar.set(year, month, day, hourOfDay, minute, second)之后也没有发生任何事情。 请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

感谢Eugen指导我。 事实证明这是价值的问题,事实上我把六月存储为第7个月而不是第6个月。我在这个月加了1个,这就是问题所在。现在我已修复它,现在工作正常。