In the sample application, it does 4 actions, Action 1~4. I want to let users create widget buttons for each different action. When the user creates a widget, the configuration activity allows the user to choose one action. Pressing the widget should do the action.
The problem is that when a widget for Action 1 is created and then another for Action 2 is created, the widget action for Action 1 seems to be overridden by that of Action 2. That is, any widget works like the last widget. How can I fix this? I have uploaded the full project to http://tempsend.com/E552810CFE (valid until August 13, 2014).
public class WidgetReceiver extends AppWidgetProvider
{
public WidgetReceiver()
{
}
private static final String WIDGET_BUTTON_CLICKED = "WidgetButtonClickedIntent";
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if (WIDGET_BUTTON_CLICKED.equals(intent.getAction()))
{
int actionID = intent.getIntExtra("ActionID", -1);
Toast.makeText(context, "Doing action " + actionID, Toast.LENGTH_SHORT).show();
Log.d("StackOverflow", "Doing action " + actionID);
}
}
@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle newOptions)
{
int actionID;
super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
Log.d("StackOverflow", "onAppWidgetOptionsChanged is called for " + appWidgetId);
Bundle bun = appWidgetManager.getAppWidgetOptions(appWidgetId);
actionID = bun.getInt("ActionID");
Log.d("StackOverflow", "The action ID is " + actionID);
RemoteViews remoteViews;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_button);
remoteViews.setTextViewText(R.id.textView, "Action " + actionID);
remoteViews.setOnClickPendingIntent(R.id.textView,
getPendingSelfIntent(context, WIDGET_BUTTON_CLICKED, actionID));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
String ids = Arrays.toString(appWidgetIds);
Log.d("StackOverflow", "onUpdate is called for " + ids);
}
protected PendingIntent getPendingSelfIntent(Context context, String action, int actionID)
{
Intent intent = new Intent(context, getClass());
intent.setAction(action);
intent.putExtra("ActionID", actionID);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}