Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and cancel a PendingIntent?

Tags:

I have an alarmManager which I am using to send notifications to the user at specific times. Since there are multiple alarms, I have multiple pending intents that I am creating and giving a unique ID, However there are certain situations in which I will need to get all the pending intents and then cancel them, so I can reset the alarms. I have tried doing this and I still cant seem to get it right so I have a couple questions:

Is this how you would correctly get and cancel a PendingIntent?

Intent intent = new Intent(con, AppointmentNotificationReciever.class); PendingIntent sender = PendingIntent.getBroadcast(con, id, intent,         PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE); am.cancel(sender); 

Does the intent need to exactly match that of the original pending intent(extras and all)?

Does the PendingIntent flag need to match that of the original pending intent?

like image 735
ninjasense Avatar asked Dec 03 '10 17:12

ninjasense


People also ask

How do I get intent from PendingIntent?

It seems you can get the Intent (retrieve the Extras originally stored with it) if it is currently held by AlarmManager if you call PendingIntent. getService (or maybe . getBroadcast ) with the same requestCode as the original and no flags, and then immediately immediately send() the intent to oneself.

What is a PendingIntent Android?

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.

Which method creates PendingIntent objects?

To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent. getBroadcast(). To perform an activity via an pending intent you receive the activity via PendingIntent. getActivity().


1 Answers

I found out that you do not actually "get" the pending intent...you have to recreate it exactly as it was when you first created it(Intent as well) and then pass it to the AlarmManager's cancel function. So the above code I posted really is not incorrect as long as thats how I first created it. Hopefully someone will find this helpful.

like image 111
ninjasense Avatar answered Sep 18 '22 11:09

ninjasense