with this tutorial i have created a widget with buttons. In the code below when i click on ButtonP1 a toast msg is seen. I try do the same with ButtonP2 also, but only the one toast msg is seen that is set to the ButtonP1. How can i do that another toast msg appears when user clicks on ButtonP2?
public class HelloWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
context.startService(new Intent(context, UpdateService.class));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetmain);
Intent active = new Intent(context, HelloWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
active.putExtra("msg", "Message for Button P1");
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.ButtonP1, actionPendingIntent);
Intent active2 = new Intent(context, HelloWidget.class);
active2.setAction(ACTION_WIDGET_RECEIVER);
active2.putExtra("msg", "Message for Button P2");
PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, active2, 0);
remoteViews.setOnClickPendingIntent(R.id.ButtonP2, actionPendingIntent2);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = intent.getExtras().getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}
else
{
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER))
{
String msg = "null";
try {
msg = intent.getStringExtra("msg");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
Toast.makeText(context, "Out: " + msg, Toast.LENGTH_SHORT).show();
}
super.onReceive(context, intent);
}
}
}
I have tried this (ACTION_WIDGET_RECEIVER2 instead of ACTION_WIDGET_RECEIVER)
Intent active2 = new Intent(context, HelloWidget.class);
active2.setAction(ACTION_WIDGET_RECEIVER2);
active2.putExtra("msg", "Message for Button P2");
PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, 0, active2, 0);
remoteViews.setOnClickPendingIntent(R.id.ButtonP2, actionPendingIntent2);
with
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER2))
{
String msg2 = "null";
try {
msg2 = intent.getStringExtra("msg2");
} catch (NullPointerException e) {
Log.e("Error", "msg = null");
}
Toast.makeText(context, "Out2: " + msg2, Toast.LENGTH_SHORT).show(); //null
}
after the same if line. In that case the msg2 variable is null.
You will have to use different request codes when retrieving the PendingIntent that will perform a broadcast. If not
PendingIntent.getBroadcast(..)will return an existing one in your case, with the message from your first Intent.