Android - AppWidget AppWidgetManager.updateAppWidget() 没有被立即调用

发布于 2024-11-26 23:51:47 字数 860 浏览 4 评论 0原文

我正在开发一个使用 RemoteViews 来显示 ListView 的应用程序小部件。为了简单起见,我将对应用程序小部件的功能进行类比:

  1. 用户将选择将应用程序小部件添加到主屏幕。选择小部件后,将启动配置活动,并且用户从食谱列表中选择一个食谱。
  2. 从配置活动中选择配方后,配置活动将广播 AppWidgetManager.ACTION_APPWIDGET_UPDATE 意图。这个意图在我的 AppWidgetProvider 类的 onReceive 方法中接收和处理。从这里,RemoteView 被实例化并传递到 AppWidgetManager.updateAppWidget() 方法中。这将继续填充配料的 ListView。

这一切都按预期工作,除非我尝试从应用程序小部件手动更新 ListView 。我设置了一个 PendingIntent 来重新启动配置活动,这也有效。不幸的是,对 AppWidgetManager.updateAppWidget() 的调用不会像在将其添加到主屏幕时启动时那样立即调用,并且 ListView 不会更新。然而,在向下滚动列表一段距离后,更新确实会被调用(我认为,直到它传递了它在缓存中加载的行数)。这会按预期触发我的 FlightBoardAppWidgetService 和 ViewsFactory。这几乎就像 updateAppWidget 被放入某个延迟加载的队列中一样。我试图查看 Android 源代码以了解 AppWidgetManager.updateAppWidget() 是如何编码的,但它似乎是隐藏的。

tl;dr: AppWidgetManager.updateAppWidget() 并不总是立即被调用,这是怎么回事?

有没有办法让ListView在实际调用时更新?我做错了什么?谢谢!

I am developing an appwidget that uses the RemoteViews to display a ListView. For simplicity's sake, I will give an analogy of the appwidget's functionality:

  1. The user will select to add the appwidget to the home screen. Upon selecting the widget, a configuration activity is launched and the user selects from one recipe from a list of recipes.
  2. Upon selecting the recipe from the configuration activity, the configuration activity broadcasts the AppWidgetManager.ACTION_APPWIDGET_UPDATE intent. This intent received and handled in the onReceive method of my AppWidgetProvider class. From here the RemoteView is instantiated and passed into the AppWidgetManager.updateAppWidget() method. This proceeds to fill in the ListView of ingredients.

This all works as expected, except when I attempt to manually update the ListView from the appwidget. I have set a PendingIntent to re-launch the configuration activity, which also works. Unfortunately, the call to AppWidgetManager.updateAppWidget() does not get called instantly as it did when being launched upon adding it to the home screen and the ListView does not get updated. The update does get called, however, after scrolling down the list a ways (until it gets passed the number of rows it has loaded in its cache, I reckon). This fires off my FlightBoardAppWidgetService and ViewsFactory as it should. It is almost as if the updateAppWidget is getting put into some lazily-loaded queue. I tried to look at the Android source code to see how AppWidgetManager.updateAppWidget() is coded, but it appears to be hidden.

tl;dr: AppWidgetManager.updateAppWidget() does not always get called instantly, what gives?

Is there any way to get the ListView to update when it is actually called? What am I doing wrong? Thanks!

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

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

发布评论

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

评论(1

梦行七里 2024-12-03 23:51:47

好吧,我终于解决了这个问题。这有点像黑客,但我最终通过声明刷新广播和更新广播解决了问题。每次我想要更新小部件时,我都会调用 updateAppWidget(),然后从接收和处理此广播的函数中,我启动另一个调用 notificationChanged 的​​广播。这一直有效!

Well, I ended up solving the problem finally. It is somewhat of a hack, but I ended up solving the problem by declaring a refresh broadcast and an update broadcast. Each time I want to update the widget I call updateAppWidget(), and then from the function that receives and handles this broadcast, I launch another broadcast that calls notifyChanged. This works all of the time!

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