android appwidget 不会从 Activity 更新
我有一个简单的应用程序小部件,我想在活动(在同一个应用程序中)中发生操作时更新它。在 onUpdate() 中,我立即更新小部件,效果很好。在我的活动中,我在 appwidget 中调用与 onUpdate() 中调用的相同静态更新方法来更新视图。小部件未更新。
我可以将代码直接跟踪到 AppWidgetManager.updateAppWidget() 方法中,这一切都很好,但小部件没有更新。
我能看到的唯一可能的区别是,当从活动的上下文与应用程序小部件的 onUpdate() 方法的上下文调用它时,传递到静态更新方法的上下文对象是不同的。然而,网上有很多这方面的例子,所以我希望它应该有效。
I have a simple appwidget and I want to update it when an action occurs in an activity (in the same app). in onUpdate(), I immediately update the widget, which works fine. In my activity, I call the same static update method in my appwidget that is called in onUpdate() to update the views. the widget is not updated.
I can trace the code right into the AppWidgetManager.updateAppWidget() method, and all this good, but the widget does not update.
The only possible difference I can see is that the context object passed into my static update method is different, when it's called from the context of an activity vs. the context of a appwidget's onUpdate() method. however, there are lots of examples on the web of this so I expect it should work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有看到您的代码,我不能 100% 确定您要如何执行此操作,但是这是我使用的方法。在我的
Activity
中,我有以下方法:其中
MyWidget
是 appwidget 的类。我可以从Activity
中的任何位置调用此方法来更新我的所有应用程序小部件。Without seeing your code I'm not 100% sure how you are trying to do it, however here is the method I use. Within my
Activity
I have the following method:Where
MyWidget
is the class of the appwidget. I can call this method from anywhere within myActivity
to update all my appwidgets.与其使用静态方法,不如利用小部件已经是广播接收器的事实,并在清单中为其注册更新意图。然后,每当您需要从活动更新它时
,只需调用清单文件的接收器标签内的
Rather than use a static method take advantage of the fact that the widget is already a broadcast receiver and register an update intent for it in your manifest. Then whenever you need to update it from the activity just call
and inside the receiver tags of your manifest file