使用this教程我创建了一个带按钮的小部件。在下面的代码中,当我点击ButtonP1时,会看到一个toast msg。我也试着用ButtonP2做同样的事情,但只看到一个设置为ButtonP1的toast msg。当用户点击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);
}
}
}
我试过这个(ACTION_WIDGET_RECEIVER2而不是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);
与
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
}
在相同的if行之后。在这种情况下,msg2变量为空。
答案 0 :(得分:2)
在检索将执行广播的PendingIntent时,您必须使用不同的请求代码。如果不是PendingIntent.getBroadcast(..)
将返回您的案例中的现有案件,并使用您的第一个意图中的消息。
private static final int REQUEST_CODE_ONE = 10;
private static final int REQUEST_CODE_TWO = 20;
....
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
....
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_ONE, active, 0);
....
PendingIntent actionPendingIntent2 = PendingIntent.getBroadcast(context, REQUEST_CODE_TWO, active2, 0);
....
}