Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling a PendingIntent

When using a PendingIntent in an AppWidgetProvider, I'm using the following code:

views.setOnClickPendingIntent( viewId,
                PendingIntent.getBroadcast( context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT ) );

So currently there's no reference kept to the PendingIntent returned by the getBroadcast method. In a specific situation I now want to cancel the PendingIntent. Is there any way of getting the PendingIntent back from the View? Or is the only way to call the cancel method of the PendingIntent afterwards by keeping a reference to it?

like image 331
Roger Rapid Avatar asked Mar 22 '12 13:03

Roger Rapid


1 Answers

Where you want to cancel it, you would do the following (somewhere else in your code base):

PendingIntent.getBroadcast(context, 0, intent, 
                           PendingIntent.FLAG_UPDATE_CURRENT).cancel();

where intent is the same one as referenced in your code above. PendingIntent.getBroadcast(...) using the PendingIntent.FLAG_UPDATE_CURRENT will return a reference to the existing one already created, or create one if it doesn't currently exist.

like image 114
Chris Knight Avatar answered Oct 25 '22 19:10

Chris Knight