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?
Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
The application specifies a PendingIntent callback (typically an IntentService) which will be called with an intent when activities are detected. The intent recipient can extract the ActivityRecognitionResult using ActivityRecognitionResult.
getBroadcast(Context context, int requestCode, Intent intent, int flags) Retrieve a PendingIntent that will perform a broadcast, like calling Context. sendBroadcast() .
FLAG_UPDATE_CURRENT : Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
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
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Change flag to PendingIntent.FLAG_ONE_SHOT
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With