SnowBEE
SnowBEE

Reputation: 923

Send EXTRA data via PendingIntent problem

Hi I'm tried to send extra data via PendingIntent.

This is my code

//**1**
    Intent intent = new Intent(context, UpdateService.class); 
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
    appWidgetId); 
            intent.putExtra(BaseConfigurationActivity.EXTRA_WIDGET_MODE, 
    2); 
            // put appWidgetId here or intent will replace an intent of 
    another widget 
            PendingIntent pendingIntent = 
    PendingIntent.getService(context, appWidgetId, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT); 
            views.setOnClickPendingIntent(R.id.gridview_button, 
    pendingIntent); 

//**2**
            intent = new Intent(context, UpdateService.class); 
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 
    appWidgetId); 
            intent.putExtra(BaseConfigurationActivity.EXTRA_WIDGET_MODE, 
    1); 
            // put appWidgetId here or intent will replace an intent of 
    another widget 
            pendingIntent = PendingIntent.getService(context, appWidgetId, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT); 
            views.setOnClickPendingIntent(R.id.listview_button, 
    pendingIntent); 

In my code it assign pendingIntent to two button gridview_button with EXTRA_WIDGET_MODE 2 and listview_button with EXTRA_WIDGET_MODE 1

when I click on gridview_button and it call UpdateService class I also got EXTRA_WIDGET_MODE value is "1"

What i'm doing wrong?

Upvotes: 11

Views: 16362

Answers (4)

danidacila
danidacila

Reputation: 62

The correct answer I guess would be use the flag

FLAG_UPDATE_CURRENT

This will reuse existing pending intents but replace their extra data.

Upvotes: 3

meizilp
meizilp

Reputation: 3921

PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);

Change flag to PendingIntent.FLAG_ONE_SHOT.

Upvotes: 8

alphonzo79
alphonzo79

Reputation: 928

I found that if the Intent that was used to create the Pending intent has any extras already in it then the new intent's extras are ignored. For example, if you follow the sample in the Android docs for building a Widget like so

Intent toastIntent = new Intent(context, StackWidgetProvider.class);
toastIntent.setAction(StackWidgetProvider.TOAST_ACTION);
toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
rv.setPendingIntentTemplate(R.id.stack_view, toastPendingIntent);

Then the line

toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

Will prevent your new intent's extras from sticking. I removed that line and my stuff worked

Upvotes: 1

SnowBEE
SnowBEE

Reputation: 923

Finally I found problem this happen because I send the same "requestCode"

PendingIntent.getService(context, appWidgetId 

It should be like this

PendingIntent.getService(context, 0 
PendingIntent.getService(context, 1 

Upvotes: 24

Related Questions