我使用下面的代码在早上 8 点开始闹钟,每 1 分钟重复一次。我想停止在中午 12 点重复此警报。我正在像这样开始警报:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent loggerIntent1 = PendingIntent.getBroadcast(this, 0,new Intent(this,AlarmReceiver.class), 0);
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, 08);
timeOff9.set(Calendar.MINUTE, 00);
timeOff9.set(Calendar.SECOND, 00);
long duration = interval * 60 * 1000;
manager.setRepeating(AlarmManager.RTC_WAKEUP,timeOff9.getTimeInMillis(), duration, loggerIntent1);
并停止使用此代码:
Calendar timeOff = Calendar.getInstance();
timeOff.set(Calendar.HOUR_OF_DAY,12);
timeOff.set(Calendar.MINUTE,00);
timeOff.set(Calendar.SECOND, 00);
manager.set(AlarmManager.RTC_WAKEUP,timeOff.getTimeInMillis(), loggerIntent1);
manager.cancel(loggerIntent1);
loggerIntent1.cancel();
但是停止代码会导致应用程序以错误的方式工作。我正在编写代码以在第一次警报后立即停止警报。是问题吗?我需要写另一个警报吗?请指导。