只需更新一个小部件 RemoteViews 而不是完全创建一个新小部件?

发布于 2024-10-08 23:11:05 字数 176 浏览 3 评论 0原文

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

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

发布评论

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

评论(3

稀香 2024-10-15 23:11:05

首先,RemoteView 不是视图。它是一组构建视图层次结构的指令。它用于在另一个进程中重新创建视图(应用程序小部件不在您的应用程序进程中执行)。因此它是可序列化和可变的。

因此,当您初始化 RemoteView 时,只需将对它的引用存储在某处,例如在您的自定义 AppWidgetProvider 的字段中。
下次您需要它时,从现场获取它并对其进行更改。要更改 TextView 中的字符串,请使用 setString(..)

remoteView.setString(textViewId, "setText", "some text for TextView")

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(..).

remoteView.setString(textViewId, "setText", "some text for TextView")
怪我闹别瞎闹 2024-10-15 23:11:05

如果您使用当前的 API,则这是 2013 年的更新。在您的 WidgetProvider 类将执行更新的方法中:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
rv = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
rv.setTextViewText(R.id.ofTextViewInWidgetLayoutXML, "Hello World");
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], rv);

请注意,它不再是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:

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
rv = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
rv.setTextViewText(R.id.ofTextViewInWidgetLayoutXML, "Hello World");
appWidgetManager.partiallyUpdateAppWidget(appWidgetIds[i], rv);

Note that it is no longer remoteView.setString but remoteView.setTextViewText

一身仙ぐ女味 2024-10-15 23:11:05

您可以更新远程视图,然后调用

ComponentName componentName= new ComponentName(context, YourClass.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

,据我所知应该更新小部件

You can update the remote views and then call

ComponentName componentName= new ComponentName(context, YourClass.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);

on, which AFAIK should update the widget

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