下面是我設置每天重複鬧鐘的代碼。現在我需要在2天后停止報警。我試圖在添加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);
你有什麼解決方案嗎? – Nik