请好好看看这个。我正在尝试为特定时间和日期设置闹钟。它似乎没有工作,我真的不知道为什么。
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.YEAR, mYear);
cal.set(Calendar.MONTH,mMonth);
cal.set(Calendar.DAY_OF_MONTH,mDay);
cal.set(Calendar.HOUR,mHour);
cal.set(Calendar.MINUTE,mMinutes);
// where mYear, mMonths, mDay, mHour and mMinutes are int values from the Date and Time picker dialogs respectively
Intent activate = new Intent(this, Alarm.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
只是为了让你知道,如果我用alerts.set()方法中的System.CurrentTimeInMillis()替换cal.getTimeInMillis(),它就有效。但那显然是我不想要的。谢谢
答案 0 :(得分:5)
白痴我!找到了解决方案。我必须先做的就是在获取实例后调用Calendar对象的clear方法。
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();