只需更新一个小部件 RemoteViews 而不是完全创建一个新小部件?
在 AppWidgetProvider 类的 onUpdate 方法中,我最终执行了大量代码,以便我可以完全重新创建一个新的 RemoteView 对象。现实是,每次更新时,我实际上只需要在 RemoteView 中的 TextView 之一中设置文本。
有什么方法可以修改特定小部件已经使用的 RemoteViews 吗?
In my onUpdate method in my AppWidgetProvider class, I ended up executing a non-trivial amount of code so that I can completely recreate a new RemoteView object. The reality is I really only need to be setting the text in one of the TextViews in the RemoteViews each time I update.
Is there any way to just modify the RemoteViews that a particular widget is already using?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,RemoteView 不是视图。它是一组构建视图层次结构的指令。它用于在另一个进程中重新创建视图(应用程序小部件不在您的应用程序进程中执行)。因此它是可序列化和可变的。
因此,当您初始化 RemoteView 时,只需将对它的引用存储在某处,例如在您的自定义 AppWidgetProvider 的字段中。
下次您需要它时,从现场获取它并对其进行更改。要更改 TextView 中的字符串,请使用
setString(..)
。First, RemoteView is not a View. It's a set of instructions that build a View hierarchy. It is used to recreate a View in another process (App Widgets do not execute in your app's process). As such it's serializable and mutable.
So, when you initialize a RemoteView just store a reference to it somewhere, e.g. in a field of your custom AppWidgetProvider.
Next time you need it, get it from field and the change something on it. For changing the string in a TextView use
setString(..)
.如果您使用当前的 API,则这是 2013 年的更新。在您的 WidgetProvider 类将执行更新的方法中:
请注意,它不再是remoteView.setString,而是remoteView.setTextViewText
This is the 2013 update if you are using current API's. In your WidgetProvider class' method that will perform an update:
Note that it is no longer remoteView.setString but remoteView.setTextViewText
您可以更新远程视图,然后调用
,据我所知应该更新小部件
You can update the remote views and then call
on, which AFAIK should update the widget