添加小部件后重新配置
将小部件添加到主屏幕后,如何重新打开小部件的配置活动?
来自 Google 搜索的以下代码不起作用,因为 extra 中的小部件 id 不会传递到 Activity:
String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
Intent configIntent = new Intent(context, Configuration.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
views.setOnClickPendingIntent(R.id.editButton, configPendingIntent);
How do I reopen the configuration activity for a widget after it has been added to the homescreen?
The following code from a Google search does not work because the widget id in the extra does not carry through to the activity:
String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
Intent configIntent = new Intent(context, Configuration.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
views.setOnClickPendingIntent(R.id.editButton, configPendingIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我晚了一年,但我只是在寻找与您相同的答案,并注意到您的代码中缺少一个标志,这是我几天前遇到的关于待决意图、附加内容的另一个问题的答案和小部件。尝试将这行代码更改
为:
PendingIntent
将重用与新意图匹配的现有缓存意图,并且“匹配”不考虑额外内容,因此可以忽略新的额外内容。添加FLAG_UPDATE_CURRENT
会使PendingIntent
将额外内容写入缓存的意图(或类似的内容)中。不管怎样,上面的内容现在对我有用(尽管我也使用了 AppWidgetManager.ACTION_APPWIDGET_CONFIGURE 作为操作名称,如果这有什么区别的话)。
OK, I'm a year too late, but I was just looking for the same answer as you and noticed a missing flag in your code, which was the answer to a different problem I had a couple of days ago concerning pending intents, extras and widgets. Try changing this line of your code:
to this:
PendingIntent
will reuse existing cached intents that match new ones and the "match" does not consider extras, so new extras can be ignored. AddingFLAG_UPDATE_CURRENT
makesPendingIntent
write the extras into the cached intent (or something like that).Anyway, the above worked for me just now (though I also used
AppWidgetManager.ACTION_APPWIDGET_CONFIGURE
as the action name, if that makes any difference).不幸的是,您不这样做:当前存在的小部件规范不支持通过小部件配置活动重新配置小部件。
您可以做的是启动小部件配置活动并手动跟踪特定的小部件 ID:新设置到位后,您可以使用适当的管理器方法手动更新小部件。但是,您将无法使用与新小部件相同的代码路径。
Unfortunately you don't: the widget spec as it currently exists does not support reconfiguration of widgets through the widget configuration activity.
What you can do is launch your widget configuration activity and track the specific widget ID manually: once the new settings are in place you can then update the widget manually using the appropiate manager method. You won't be able to use the same code path as a new widget does, however.