How can I cancel one AlarmManager on a scheduled time, which was already started. I am starting one AlarmManager like this.
I want to cancel it after 5 hours. How can I do this?
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 30);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);
set a another alarm for 5 hours from now. when this alarm alarm goes off then cancel the first alarm.
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentForCancelling = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intentForCancelling,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.HOUR_OF_DAY, 5);
service.set(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), pending)
and, from ReceiverForCancelling
you can cancel the first alarm.
edit::
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReceiverForCancelling.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
service.cancel(pending);