3

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.

Community
  • 1
  • 1
user3997118
  • 71
  • 2
  • 10

5 Answers5

4

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);
MHP
  • 2,613
  • 1
  • 16
  • 26
3

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);
A.AL
  • 135
  • 3
  • 12
1

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);
jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • I have tried above, setting alarm is working and also shows notification but deleting causes all alarms to delete. I have checked by break point debugging that my list's items values are always unique wrt position. [gist here](https://gist.github.com/talhahasanzia/42a3e9ef0fa239cda06511a9571c18b3) Kindly have a look at my methods. – Talha Sep 05 '17 at 15:21
1

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();`
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25
Ibrahim Sušić
  • 438
  • 3
  • 20
  • Actually, it's not needed to call `p.cancel()`. All you need is `am.cancel(p)`. Also, using `FLAG_CANCEL_CURRENT`, or not, is strictly for updating or removing the Intent extras associated with the PendingIntent. See this: http://stackoverflow.com/questions/14029400/flag-cancel-current-or-flag-update-current/14036621#14036621 – Daniel Nugent May 07 '15 at 21:20
  • yup, PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT) PendingIntent.FLAG_CANCEL_CURRENT – Ibrahim Sušić May 07 '15 at 21:24
  • That being said, if you create an alarm with a PendingIntent that uses `FLAG_CANCEL_CURRENT`, you have to use it again when cancelling the alarm. – Daniel Nugent May 07 '15 at 21:26
  • *Never* use FLAG_CANCEL_CURRENT when working with PendingIntents that are used by alarms. See http://stackoverflow.com/questions/14029400/flag-cancel-current-or-flag-update-current/39926854#39926854 for more details. – ctate Feb 17 '17 at 23:21
  • I have tried above, setting alarm is working and also shows notification but deleting causes all alarms to delete. I have checked by break point debugging that my list's items values are always unique wrt position. I have also tried adding `p.cancel();` [gist here](https://gist.github.com/talhahasanzia/42a3e9ef0fa239cda06511a9571c18b3) Kindly have a look at my methods. – Talha Sep 05 '17 at 15:22
0

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 e.g 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 e.g 1001, intent, 0); alarmMgr.cancel(alarmIntent);

I hope this will help you.

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11
  • Thank you very much :).How can i set different ID and get the ID for different alarms? – user3997118 May 07 '15 at 21:31
  • U Welcome.If you are setting multiple alarms then set those alarms in a loop and use the counter of loop as request code in pending intent.and when you want to remove then use the same logic.again start a loop.and use the counter as request code in pending intent.But take care of one thing that the logic should be same at both time. – Zohaib Hassan May 08 '15 at 20:17