A有putExtra
的问题!
int identif = context.getResources().getIdentifier("widget_button" + b, "id", context.getPackageName());
System.out.println("dump piar: " + "widget_button" + b + " " + serverList.get(i).id);
switch (b) {
case 1:
Intent widgetButton1 = new Intent(context, ServerDetailsActivity.class);
widgetButton1.setAction(ACTION_WIDGET_SERVER1);
widgetButton1.putExtra("serverId", String.valueOf(serverList.get(i).id));
PendingIntent configPendingIntent1 = PendingIntent.getActivity(context, 0, widgetButton1, 0);
remoteViews.setOnClickPendingIntent(identif, configPendingIntent1);
break;
case 2:
Intent widgetButton2 = new Intent(context, ServerDetailsActivity.class);
widgetButton2.setAction(ACTION_WIDGET_SERVER2);
widgetButton2.putExtra("serverId", String.valueOf(serverList.get(i).id));
PendingIntent configPendingIntent2 = PendingIntent.getActivity(context, 0, widgetButton2, 0);
remoteViews.setOnClickPendingIntent(identif, configPendingIntent2);
break;
case 3:
Intent widgetButton3 = new Intent(context, ServerDetailsActivity.class);
widgetButton3.setAction(ACTION_WIDGET_SERVER3);
widgetButton3.putExtra("serverId", String.valueOf(serverList.get(i).id));
PendingIntent configPendingIntent3 = PendingIntent.getActivity(context, 0, widgetButton3, 0);
remoteViews.setOnClickPendingIntent(identif, configPendingIntent3);
break;
}
将字符串放到按钮上,当我按下第二个按钮时:
Intent lastIntent = getIntent();
int serverId = Integer.parseInt(lastIntent.getStringExtra("serverId"));
System.out.println("dump serverId: " + serverId);
得到错误的ID。但我不知道为什么。如果我触摸第二个按钮,我给了id" 3"不是" 4"
dump piar: widget_button1 1
dump piar: widget_button2 4
dump piar: widget_button3 5
dump serverId: 3
帮忙。 THX。
修改
如果我删除了putExtra,我也可以使用getStringExtra获取serverId,但我不知道为什么。 (如果我删除了setOnClickPendingIntent,则按钮不可触摸。)
解决:
如果使用此标记:PendingIntent.FLAG_UPDATE_CURRENT
工作正常!
PendingIntent configPendingIntent3 = PendingIntent.getActivity(context, 0, widgetButton3, PendingIntent.FLAG_UPDATE_CURRENT);