How can I make a pending intent unique based on extras

satya - Thursday, December 15, 2011 2:22:39 PM

Setting an extra on an intent


onListClickIntent.putExtra(
        AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

satya - Thursday, December 15, 2011 2:23:53 PM

uniqueness of pending intents and extras: toUri()

A pending intent does not take into account any subsequent extras you set on the underlying intent unless that intent is unique after taking into account the extras. However, intents don't take into account their extras when considering if they are unique. To circumvent this issue you need to use a method called toUri() on an intent.

This toUri() method takes all the extras of an intent and then makes a long string representing this intent with extras at the end. When you take this long string and set it as the data portion of the same intent, you essentially made this intent unique. This is because an intent will take its data portion under consideration for uniqueness

satya - Thursday, December 15, 2011 2:24:12 PM

Example of toUri()


onListClickIntent.setData(
    Uri.parse(
        onListClickIntent.toUri(Intent.URI_INTENT_SCHEME)));

satya - Thursday, December 15, 2011 2:28:00 PM

Getting a unique pending intent out of it


PendingIntent onListClickPendingIntent = 
    PendingIntent.getBroadcast(context, 0,                    
        onListClickIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT);