I have multiple widgets. On update for each i start a service Code in service:
for (int widgetId : allWidgetIds) {
// Register an onClickListener for DisplayActivity
Intent clickIntentUpdate1 = new Intent(
this.getApplicationContext(), DisplayActivity.class);
clickIntentUpdate1.putExtra("widgetid", widgetId);
clickIntentUpdate1.putExtra(DisplayActivity.WHAT, what);
clickIntentUpdate1.putExtra(DisplayActivity.WHAT_COLOR, prefs
.getString(QuoteConfigure.PREF_PREFIX_KEY_QUOTE_COLOR
+ widgetId, "Transparent"));
clickIntentUpdate1.putExtra(DisplayActivity.WHO, who);
clickIntentUpdate1.putExtra(DisplayActivity.WHO_COLOR, prefs
.getString(
QuoteConfigure.PREF_PREFIX_KEY_QUOTE_BY_COLOR
+ widgetId, "Transparent"));
clickIntentUpdate1.putExtra(
DisplayActivity.BG_COLOR,
prefs.getString(QuoteConfigure.PREF_PREFIX_KEY_QUOTE_BG
+ widgetId, "Transparent"));
PendingIntent pendingIntentUpdate1 = PendingIntent.getActivity(
getApplicationContext(), 0, clickIntentUpdate1,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.what,
pendingIntentUpdate1);
Log.d("quoteSERVICE", what + " -> " + who);
Log.d("quoteSERVICE", "what inside intent:"+clickIntentUpdate1.getExtras().getString(DisplayActivity.WHAT));
Log.d("quoteSERVICE", "widgetid:"+widgetId);
Log.d("quoteSERVICE", "--------------------");
// Register an onClickListener for Who search
Intent clickIntentSearch = new Intent(Intent.ACTION_VIEW);
clickIntentSearch.setData(Uri
.parse("http://www.google.com/search?q=" + who));
PendingIntent pendingIntentSearch = PendingIntent.getActivity(
getApplicationContext(), 0, clickIntentSearch, 0);
remoteViews.setOnClickPendingIntent(R.id.who,
pendingIntentSearch);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
Now the problem is:
I have two widgets and when they update intent on R.id.who is fired as it is supposed to be with the required data. However the event for R.id.what is not fired as it is supposed to be. The data sent on this event is the last values of what and who sent(i.e. the ones in the last widget to be updated.)
I could be wrong, but IIRC Android has a bug/quirk/feature that does not let you have multiple identical outstanding PendingIntents.
That means if you create two pending intents that differ only by extras, the first one will be forgotten and when the intent fires it will send the last intent that was set.
I think you can differentiate them by setting a bogus “Action”, that while unused, causes android to not assume the two intents are the same, and your code will work how you expect.
Can you try inserting this statement below, and see if that fixes your problem?
The action string itself does not matter, you just need each appwidget’s intent to have a unique string, so that the intents are not combined.