各位好,我在我的APP裡面建立了兩個AlarmManager,
這是我的程式碼:
@RequiresApi(api = Build.VERSION_CODES.N)
private void morning_alarm(Calendar calendarTime) {
intent = new Intent(Setting_Activity.this,
AlarmNotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if(calendarTime.before(now)) {
calendarTime.add(Calendar.DATE, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
calendarTime.add(Calendar.DATE,-1);
}
else
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
private void evening_alarm(Calendar calendarTime) {
intent = new Intent(Setting_Activity.this,
AlarmNotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
if(calendarTime.before(now)) {
calendarTime.add(Calendar.DATE, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
calendarTime.add(Calendar.DATE,-1);
}
else
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendarTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
但後來我設了一個switch想要做關閉,當我的switch關閉時,
manager.cancel(pendingIntent),但似乎沒辦法同時關閉兩個,
想問一下該怎麼改寫比較好?
或是有其他辦法能夠同時關閉兩個AlarmManager
新手上路請多見諒,先謝謝各位了。