I am building a reminder app using alarmManger but I don't know how to delete a specific alarm.
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
The Receiver is a normal and simple Receiver.
My problem is that I want to edit/remove an alarm that has been added. I know conceptually how to remove an alarm , but I can't remove a specific alarm in alarmManger when several have been added.
by Id that you set in alarm manager you can access specific alarm, in your code:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
at this line choose ID separate from each other , then you can get specific alarm by it's id
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), ID, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
You need to have an unique id ( requestCode
), specific to the alarm you want to cancel. I've called that id customId
. Pass that id to your intent (rather than passing 0 as you currently do).
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), customId, intent, 0);
store customId however you want but you can later get the alarm using the flag PendingIntent.FLAG_UPDATE_CURRENT
:
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), customId, intent, PendingIntent.FLAG_UPDATE_CURRENT)
And then you can do what you want to alarmIntent
. In your code your Alarm Manager is called manager
so you would call:
manager.cancel(alarmIntent);
You have to set unique requestCode for the PendingIntent :
to set:
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
11111, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
to cancel :
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,
11111, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
When canceling the AlarmManager do not use a PendingIntent with a flag of FLAG_CANCEL_CURRENT. Instead, cancel the PendingIntent explicitly after canceling the alarm:
am = (AlarmManager) getSystemService(getApplicationContext().ALARM_SERVICE);
Intent i = new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent p = PendingIntent.getBroadcast(getApplicationContext(), 0, i, 0);
am.cancel(p); p.cancel();`
That's simple.Just give a specific request code on creating on creating pending intent for each alarm.like PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), your specific code eg 1001, intent, 0);
and when you want to delete the alarm just create a pending intent with that specific code and cancel it. PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), your specific code eg 1001, intent, 0); alarmMgr.cancel(alarmIntent);
I hope this will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.