AppWidgetProvider:变量遇到问题

发布于 2024-11-15 06:23:55 字数 433 浏览 2 评论 0原文

我已经为主屏幕构建了一个小部件,并在我的 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 技术交流群。

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

发布评论

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

评论(3

我是有多爱你 2024-11-22 06:23:55

如果我正确理解了这个问题,那么您需要为每个小部件实例拥有该值的唯一实例。

对于这种行为,您需要将数据存储在静态对象中,除以 appWidgetId。对我来说,最好的方法是使用自定义对象的静态 HashMap,如下所示:

private static class MyValues{
    private final int iPicNum = 0;
    private final boolean bClosed = false;

    public MyValues(int iPicNum, boolean bClosed) {
        this.iPicNum = iPicNum;
        this.bClosed = bClosed;
    }
    public int getiPicNum() {
        return iPicNum;
    }
    public boolean isbClosed() {
        return bClosed;
    }        
}

在您的 AppWidgetProvider 类中:

private static HashMap<int,MyValues> mValues = new HashMap<int,MyValues>;

当您第一次需要为小部件保存一些数据时:

mValues.put(appWidetId, new MyValues(iPicNum,bClosed);

获取数据:

MyValues values = mValues.get(appWidgetId);
if (values != null){
   int iPicNum = values.getiPicNum();
   boolean bClosed = values.isbClosed();
}

并且不要忘记清除onDeleted() 方法中未使用的数据(当从屏幕上删除单个小部件实例时调用):

public void onDeleted(Context context, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        if (appWidgetId != -1) {
            mValues.remove(appWidgetId);
        }
    }
}

希望这会有所帮助。

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:

private static class MyValues{
    private final int iPicNum = 0;
    private final boolean bClosed = false;

    public MyValues(int iPicNum, boolean bClosed) {
        this.iPicNum = iPicNum;
        this.bClosed = bClosed;
    }
    public int getiPicNum() {
        return iPicNum;
    }
    public boolean isbClosed() {
        return bClosed;
    }        
}

In your AppWidgetProvider class:

private static HashMap<int,MyValues> mValues = new HashMap<int,MyValues>;

When you first need to save some data for widget:

mValues.put(appWidetId, new MyValues(iPicNum,bClosed);

To obtain data:

MyValues values = mValues.get(appWidgetId);
if (values != null){
   int iPicNum = values.getiPicNum();
   boolean bClosed = values.isbClosed();
}

And don't forget to clear unused data in onDeleted() method (it called, when single instanse of widget is removed from screen):

public void onDeleted(Context context, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        if (appWidgetId != -1) {
            mValues.remove(appWidgetId);
        }
    }
}

Hope this helps.

甜警司 2024-11-22 06:23:55

为什么不在 MyWidget 中覆盖 onDeleted() 并在调用时重置您的值(不过请记住调用 super.onDeleted() )。

Why not override onDeleted() in MyWidget and reset your values whenever this is called (remember to call super.onDeleted() though).

哭了丶谁疼 2024-11-22 06:23:55

变量共享值的原因是它们被声明为静态的。在 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.

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