我正在使用以下代码在android中将重复警报设置为每隔12点触发一次,但即使一次也不会触发。 我需要此警报从第二天凌晨12点开始触发,因此我在日历对象中添加了一个日期。
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
calendar.add(Calendar.DATE,1);
Utils.printLog("date from repeating alarm "+calendar.getTime());
Intent startIntent = new Intent(context, RepeatingAlarmReceiver.class);
PendingIntent startPIntent = PendingIntent.getBroadcast(context, 15, startIntent, 0);
AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmMgr != null) {
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, startPIntent);
}
我通过断点进行了测试,发现alarmMgr
设置时不为null。
RepeatingAlarmReceiver.class
也在工作。