1

我有一个应该在每天凌晨 12 点运行的闹钟(它不是重复的闹钟,我每次需要 cz 时都会设置它,在某些情况下我不想让它运行)

Intent myIntent = new Intent(AlarmService.this, AlarmService.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.AM_PM, Calendar.AM);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

奇怪的是,如果我手动将日期/时间更改为第二天的凌晨 12 点,就会触发警报。但是,如果我将时间设置为今天晚上 11:59 并等到凌晨 12 点,则没有任何反应,如果时间/日期是自动设置的,它也不会在上午 12 点触发。知道为什么会这样,或者我该如何解决?谢谢

4

1 回答 1

0

你的代码有一些问题,看看这个我在它完美之前使用它,它会改进你的代码。

于 2012-11-12T08:08:36.583 回答