添加小部件后重新配置

发布于 2024-11-17 09:19:46 字数 523 浏览 2 评论 0原文

将小部件添加到主屏幕后,如何重新打开小部件的配置活动?

来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2024-11-24 09:19:48

好吧,我晚了一年,但我只是在寻找与您相同的答案,并注意到您的代码中缺少一个标志,这是我几天前遇到的关于待决意图、附加内容的另一个问题的答案和小部件。尝试将这行代码更改

PendingIntent configPendingIntent
    = PendingIntent.getActivity(context, 0, configIntent, 0);

为:

PendingIntent configPendingIntent
     = PendingIntent.getActivity(context, 0, configIntent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

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:

PendingIntent configPendingIntent
    = PendingIntent.getActivity(context, 0, configIntent, 0);

to this:

PendingIntent configPendingIntent
     = PendingIntent.getActivity(context, 0, configIntent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent will reuse existing cached intents that match new ones and the "match" does not consider extras, so new extras can be ignored. Adding FLAG_UPDATE_CURRENT makes PendingIntent 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).

烟花肆意 2024-11-24 09:19:48

不幸的是,您不这样做:当前存在的小部件规范不支持通过小部件配置活动重新配置小部件。

您可以做的是启动小部件配置活动并手动跟踪特定的小部件 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文