【发布时间】:2019-11-06 08:19:11
【问题描述】:
在我从配置活动中按下后,我需要删除一个主屏幕小部件实例,因为我希望它被完全删除,而不是因为issue 2539 而留在“边缘”中。所以最好是以下修复之一:
- 修复问题 2539 并让小部件实例从主屏幕和“边缘”中优雅地消失
- 让程序员通过 AppWidgetHost 使用正确的 id 引用主屏幕来执行此操作,(证明此安全漏洞)。 (有趣的尝试描述了here)
现在这些都有可能吗?
【问题讨论】:
在我从配置活动中按下后,我需要删除一个主屏幕小部件实例,因为我希望它被完全删除,而不是因为issue 2539 而留在“边缘”中。所以最好是以下修复之一:
现在这些都有可能吗?
【问题讨论】:
我自己解决了,只处理两个布尔标志。 这是我在扩展 AppWidgetProvider 的类上所做的
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences settings = context.getSharedPreferences(SHARED_PREFERENCES, 0);
for(int widgetId:appWidgetIds)
{
boolean configured = settings.getBoolean(CONFIGURED_PREFERENCE+widgetId, false); //In order to skip initial UpdateService
boolean widget= settings.getBoolean(WIDGET_PREFERENCE+widgetId, false);
if(!widget && configured) continue; // In order to skip phantom Widgets update
if(!configured)
{
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(CONFIGURED_PREFERENCE+widgetId, true);
editor.commit();
}
else
{ Intent updateService=new Intent(context, UpdateService.class);
updateService.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,widgetId);
context.startService(updateService);
}
}
}
【讨论】: