I have a widget defined in my Android app that maps to a specific lightswitch which is chosen by a configuration Activity. Each instance of the widget can map to a different lightswitch and tapping the widget toggles the light on and off. Here's the code from the Activity that sets up the widget:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() != R.id.action_choose) {
return super.onOptionsItemSelected(item);
RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.device_widget);
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_off);
SharedPreferences.Editor prefs = getApplicationContext().getSharedPreferences(MyWidgetService.WIDGET_PREFS, MODE_PRIVATE).edit();
prefs.putInt(MyWidgetService.WIDGET_DEVICE_ID + Integer.toString(mAppWidgetId), mDeviceId);
prefs.apply();
Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
clickIntent.putExtra(MyWidgetService.ON_LEVEL, 100);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetOnOffButton, pendingIntent);
mAppWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
}
I've verified that the mAppWidgetId is the new ID for the widget. Here's my AppWidgetProvider's code, I use the onReceive method so I can access the extras in the Intent, but I have an onUpdate and onDelete defined for any Intents that fall outside the normal click conditions:
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
int onLevel = intent.getIntExtra(MyWidgetService.ON_LEVEL, -1);
if(action == AppWidgetManager.ACTION_APPWIDGET_UPDATE && widgetId > -1 && onLevel > -1) {
Intent newIntent = new Intent(context.getApplicationContext(), WishFullWidgetService.class);
newIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
newIntent.putExtra(MyWidgetService.ON_LEVEL, onLevel);
context.startService(newIntent);
}
super.onReceive(context, intent);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
return true;
}
And this is the code from MyWidgetService. Each time a widget is clicked, I reset its OnClickPendingIntent with its own WidgetId and OnLevel, however all the widgets fire off the PendingIntent with the latest widget's ID and OnLevel.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
int onLevel = intent.getIntExtra(ON_LEVEL, -1);
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.device_widget);
Intent clickIntent = new Intent(this.getApplicationContext(), WishFullWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
if(onLevel > 0) {
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_on);
clickIntent.putExtra(ON_LEVEL, 0);
} else {
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_off);
clickIntent.putExtra(ON_LEVEL, 100);
}
if(onLevel > -1) {
int deviceId = mPrefs.getInt(WIDGET_DEVICE_ID + Integer.toString(widgetId), -1);
sendMessageToDevice(onLevel, deviceId);
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetOnOffButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
At this point, if I have Widget 1 and Widget 2, clicking Widget 1 will fire the Intent containing the ID and OnLevel of Widget 2. How can I update the OnClickPendingIntent of each widget independently without the newest one overwriting the others?