Android - 如何处理 appWidgets 容器的重新启动?

发布于 2024-12-29 13:23:14 字数 415 浏览 1 评论 0原文

我正在创建 appWidget 容器和 appWidgets。 我有一个问题,我认为是由于创建的 appWidgets 造成的 - 每次我重新启动(杀死然后打开)appWidget 容器时,都会显示 appWidget,但它不再处理点击。

这种情况发生在每个 appWidget 容器(启动器甚至 widgetLocker)上,所以这就是为什么我怀疑这是因为 appWidget 造成的。

我在网上尝试了很多教程,但找不到有关此问题的任何参考资料。

也许按钮的意图(好的,pendingIntents)的准备不应该单独在 onUpdate 方法上创建?如果是的话,我应该在哪里添加它?我已将其添加到 onReceive 上,但需要很长时间才能发生(如果有的话)。

我本想在这里添加一些代码,但是在线的任何代码都给了我相同的结果。

我该如何修复它?

I'm creating both an appWidget container and appWidgets.
I have a problem which I think is because of the created appWidgets - each time I restart (kill and then open) the appWidget container, the appWidget is shown, but it no longer handles clicks.

This occurs on every appWidget container (launchers and even widgetLocker), so that's why I suspect it's because of the appWidget.

I've tried many tutorials online, but I couldn't find any reference about this problem.

Maybe the preparation of the intents (OK, pendingIntents) of the buttons should not be created on the onUpdate method alone? If so, where should I add it? I've added it on the onReceive, but it takes a very long time till that occurs (if at all).

I would have added some code here, but any code online gave me the same results.

How can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

暖树树初阳… 2025-01-05 13:23:14

好吧,我不确定是什么导致了这个问题,但这是解决方案。

简而言之:

onUpdate(...)
{
  super.onUpdate(context, appWidgetManager, appWidgetIds);
  for (int appWidgetId: appWidgetIds)
  {
    // Prepare remoteViews, including registration of their clicks events...
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
  }
}

onReceive(...)
{
  if (intent.getAction().equals(ACTION_BUTTON_CLICKED))
  {
    // Get needed data from the customized intent.
    // Prepare remoteViews of what should be updated, including
    // registration of their clicks events...
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
  }
}

OK, I am not sure what caused the problem, but here is the solution.

In short:

onUpdate(...)
{
  super.onUpdate(context, appWidgetManager, appWidgetIds);
  for (int appWidgetId: appWidgetIds)
  {
    // Prepare remoteViews, including registration of their clicks events...
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
  }
}

onReceive(...)
{
  if (intent.getAction().equals(ACTION_BUTTON_CLICKED))
  {
    // Get needed data from the customized intent.
    // Prepare remoteViews of what should be updated, including
    // registration of their clicks events...
    appWidgetManager.updateAppWidget(appWidgetId, remoteViews)
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文