AppWidgetProvider:变量遇到问题
我已经为主屏幕构建了一个小部件,并在我的 AppWidgetProvider 类中声明了一些变量。这些变量是整数和布尔值。
我遇到的问题(它基本上是一个java编程问题)是,无论我分配给我的变量什么值;当我从主屏幕删除小部件,然后在稍后的某个时间将小部件添加回主屏幕时,变量仍保留上次小部件在主屏幕上时的值。我按如下方式声明变量:
public class MyWidget extends AppWidgetProvider {
static int iPicNum = 0;
static Boolean bClosed = false;
...
每次将小部件从屏幕上删除并将其重新放回屏幕上时,如何重置变量的值。或者,如果用户希望屏幕上有多个小部件实例,则变量不应在它们之间共享值。它们应该相互独立。
谢谢 法拉兹·阿扎尔
I've built a widget for home screen and declared some variables in my AppWidgetProvider class. These variables are integers and booleans.
The problem that I'm having (its basically a java programming problem) is that whatever value I assign to my variables; when I delete the widget from the home screen and then later at some time I add the widget back to the home screen, the variables are still holding the values as of the last time the widget was on home screen. I declare the variables as follows:
public class MyWidget extends AppWidgetProvider {
static int iPicNum = 0;
static Boolean bClosed = false;
...
How do I reset the values of my variables every time the widget is removed from the screen and put it back on the screen again. Or if the user wishes to have more than one instances of the widget on the screen, the variables shouldn't be sharing values between them. They should be independent from each other.
Thank you
Faraz Azhar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我正确理解了这个问题,那么您需要为每个小部件实例拥有该值的唯一实例。
对于这种行为,您需要将数据存储在静态对象中,除以 appWidgetId。对我来说,最好的方法是使用自定义对象的静态 HashMap,如下所示:
在您的 AppWidgetProvider 类中:
当您第一次需要为小部件保存一些数据时:
获取数据:
并且不要忘记清除onDeleted() 方法中未使用的数据(当从屏幕上删除单个小部件实例时调用):
希望这会有所帮助。
If I have correctly understood the problem, you need to have unique instances of this values for each widget instance.
For such behavior you need to store data in static object, divided by appWidgetId. As for me, the best way to do this, is to use static HashMap of custom objects, like this:
In your AppWidgetProvider class:
When you first need to save some data for widget:
To obtain data:
And don't forget to clear unused data in onDeleted() method (it called, when single instanse of widget is removed from screen):
Hope this helps.
为什么不在
MyWidget
中覆盖onDeleted()
并在调用时重置您的值(不过请记住调用super.onDeleted()
)。Why not override
onDeleted()
inMyWidget
and reset your values whenever this is called (remember to callsuper.onDeleted()
though).变量共享值的原因是它们被声明为静态的。在 Java 中,当你将一个变量声明为 static 时,这意味着它只会有一个实例。以假设的 PolarBear 类为例。该类可以有一个静态 Color 变量,设置为“白色”。这意味着每次您请求 PolarBear.bearColor 时,您都会返回白色。但它比这稍微微妙一些。您不会获得新对象,您每次都会获得相同的对象。
The reason that the variables are sharing values is that they're declared static. In Java, when you declare a variable static, that means that there will only ever be one instance of it. Take for example a hypothetical class PolarBear. This class could have a static Color variable, set to "white". This means that everytime you ask for PolarBear.bearColor, you'll get back the color White. It's slightly more subtle than that though. You won't get a new object, you'll get the same object, everytime.