我一直在尝试为特定日期设置 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)
什么也没有发生。请帮忙!谢谢!