以下是我每天设置重复警报的代码。现在我需要在 2 天后停止闹钟。我试图在增加 2 天后停止警报,但它不起作用。请任何人帮助我。
Intent myIntent = new Intent(this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour1);
calendar.set(Calendar.MINUTE, min1);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
Toast.makeText(this, "Start Daily Alarm", Toast.LENGTH_LONG).show();
//trying to cancel after 2 days
// add days to current date using Calendar.add method
calendar.add(Calendar.DATE, 2);
pendingIntent = PendingIntent.getService(this, (int) System.currentTimeMillis(), myIntent, 0);
AlarmManager alarmManagerstop = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManagerstop.cancel(pendingIntent);