9

I have used the same pendingIntent to set multiple alarms using different intentId for each. the alarm is working. Now i want to cancel a particular alarm. If i use the cancel() method i would end up cancelling all the alarms. I want only a specific one to be deleted. Also the user should be able to cancel this alarm even during a second or a third launch. As in when i launch it the second time, i won't be having the same pendingIntent object. Would i have to persist the pendingIntent object? If so, how? and how do i cancel a single alarm from multiple alarms?

flamesavor
  • 129
  • 3
  • 8
  • I don't know of a method to cancel a single alarm aout of a set of alarms with the same pending intent. But you could mark a particular alarm time as canceled using some method of persistence (shared preferences, a file, sqlite, ...) and in the code that is called through the alarm intent evaluate the saved information to determine whether the alarm has been cancelled. Or you may cancel all the alarms and set all but one afterwards (if you still know all the times). BTW, you do not need the original pending intent, you may create a new one. – Stefan Jan 16 '12 at 08:42

2 Answers2

11

You can do it like this,

In your Pending Intent you can pass a unique ID in place of requestCode

PendingIntent pi = PendingIntent.getBroadcast(context, unique_id, i, 0);

And to cancel you can use the same unique ID to cancel it, using the same Pending Intent.

am.cancel(pi);

For getting more information you can just use StackOverflow or Google, for now I think this answer will do for you. :)

Community
  • 1
  • 1
Lalit Poptani
  • 65,659
  • 21
  • 155
  • 238
  • If you set your unique_id to your alarm time in milliseconds, you can guarantee it will be unique. Therefor you can easily cancel it. I verified this works. The calendar method is calendar.getTimeInMillis() – bschwagg Dec 23 '14 at 06:42
  • can you please help me to solve this https://stackoverflow.com/questions/51542099/cant-stop-the-ringing-alarm-from-another-activity#51542099 @LalitPoptani – Zhu Jul 26 '18 at 15:58
3

Here is a kind of hack to do this with the explanation.

First of all you should create a unique intent for the pending intent. For this purpose you can create a custom data field of the intent for your application. I do this in the following way:

Intent intent = new Intent();
intent.setAction(ExampleAppWidgetProvider.MY_INTENT_ACTION);
Uri data = Uri.withAppendedPath(
                Uri.parse("myapp://myapp/Id/#"),
                String.valueOf(intentId));
intent.setData(data);

In your case intentId will be yours unique identifier of the intent.

Then you create alarmManager notification as usual. To cancel an alarm you should do the following steps. At first you should create an intent as in the previous code sample. Then you create pending intent based on this intent (you also create the same pending intent for alarm). And then you cancel this alarm:

Intent intent = new Intent();
intent.setAction(ExampleAppWidgetProvider.MY_INTENT_ACTION);
Uri data = Uri.withAppendedPath(
                Uri.parse("myapp://myapp/Id/#"),
            String.valueOf(intentId));
intent.setData(data);


PendingIntent pendingIntent = PendingIntent.getBroadcast(
                context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Yury
  • 19,498
  • 7
  • 52
  • 79