为啥 AppWidgetManager 会更新所有 RemoteViews for Widgets 的 Intent?
Posted
技术标签:
【中文标题】为啥 AppWidgetManager 会更新所有 RemoteViews for Widgets 的 Intent?【英文标题】:Why does AppWidgetManager update Intent of all RemoteViews for Widgets?为什么 AppWidgetManager 会更新所有 RemoteViews for Widgets 的 Intent? 【发布时间】:2015-07-08 18:42:50 【问题描述】:我在我的 android 应用程序中定义了一个小部件,该小部件映射到由配置活动选择的特定灯开关。小部件的每个实例都可以映射到不同的灯开关,点击小部件可以打开和关闭灯。 下面是 Activity 中设置小部件的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item)
if (item.getItemId() != R.id.action_choose)
return super.onOptionsItemSelected(item);
RemoteViews remoteViews = new RemoteViews(this.getPackageName(), R.layout.device_widget);
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_off);
SharedPreferences.Editor prefs = getApplicationContext().getSharedPreferences(MyWidgetService.WIDGET_PREFS, MODE_PRIVATE).edit();
prefs.putInt(MyWidgetService.WIDGET_DEVICE_ID + Integer.toString(mAppWidgetId), mDeviceId);
prefs.apply();
Intent clickIntent = new Intent(this.getApplicationContext(), MyWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
clickIntent.putExtra(MyWidgetService.ON_LEVEL, 100);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetOnOffButton, pendingIntent);
mAppWidgetManager.updateAppWidget(mAppWidgetId, remoteViews);
我已验证 mAppWidgetId 是小部件的新 ID。这是我的 AppWidgetProvider 的代码,我使用了 onReceive 方法,因此我可以访问 Intent 中的额外内容,但我为任何超出正常点击条件的 Intent 定义了 onUpdate 和 onDelete:
@Override
public void onReceive(Context context, Intent intent)
String action = intent.getAction();
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
int onLevel = intent.getIntExtra(MyWidgetService.ON_LEVEL, -1);
if(action == AppWidgetManager.ACTION_APPWIDGET_UPDATE && widgetId > -1 && onLevel > -1)
Intent newIntent = new Intent(context.getApplicationContext(), WishFullWidgetService.class);
newIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
newIntent.putExtra(MyWidgetService.ON_LEVEL, onLevel);
context.startService(newIntent);
super.onReceive(context, intent);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
return true;
这是来自 MyWidgetService 的代码。每次单击小部件时,我都会使用其自己的 WidgetId 和 OnLevel 重置其 OnClickPendingIntent,但是所有小部件都会使用最新的小部件 ID 和 OnLevel 触发 PendingIntent。
@Override
public int onStartCommand(Intent intent, int flags, int startId)
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
int onLevel = intent.getIntExtra(ON_LEVEL, -1);
RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.device_widget);
Intent clickIntent = new Intent(this.getApplicationContext(), WishFullWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
if(onLevel > 0)
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_on);
clickIntent.putExtra(ON_LEVEL, 0);
else
remoteViews.setImageViewResource(R.id.widgetOnOffButton, android.R.drawable.star_big_off);
clickIntent.putExtra(ON_LEVEL, 100);
if(onLevel > -1)
int deviceId = mPrefs.getInt(WIDGET_DEVICE_ID + Integer.toString(widgetId), -1);
sendMessageToDevice(onLevel, deviceId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.widgetOnOffButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
stopSelf();
return super.onStartCommand(intent, flags, startId);
此时,如果我有 Widget 1 和 Widget 2,单击 Widget 1 将触发包含 Widget 2 的 ID 和 OnLevel 的 Intent。如何独立更新每个小部件的 OnClickPendingIntent 而不会覆盖其他小部件?
【问题讨论】:
【参考方案1】:传递一个唯一的int作为pendingIntent的第一个标志(第二个参数),最好是widgetID。
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), uniqueID, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
这将导致android不覆盖现有的pendingIntent,而是创建一个新的。
这里有一个 link 到另一个有类似问题的帖子。
【讨论】:
以上是关于为啥 AppWidgetManager 会更新所有 RemoteViews for Widgets 的 Intent?的主要内容,如果未能解决你的问题,请参考以下文章