avec this tutoriel J'ai créé un widget avec des boutons. Dans le code ci-dessous quand je clique sur ButtonP1 un toast msg est vu. J'essaye de faire de même avec ButtonP2, mais seul le toast msg est vu qui est mis à ButtonP1. Comment puis-je faire qu'un autre message toast apparaisse lorsque l'utilisateur clique sur ButtonP2?Bouton Widget Android
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);
}
}
}
J'ai essayé (ACTION_WIDGET_RECEIVER2 au lieu de 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);
avec
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
}
après la même si la ligne. Dans ce cas, la variable msg2 est nulle.