在 Blackberry Java 中开发持久存储

发布于 2025-01-04 10:24:33 字数 3229 浏览 3 评论 0 原文

我目前有代码可以在应用程序中的两个入口点之间共享变量。该变量是 iconCount 变量,用于指示用户有多少条通知显示在主屏幕图标旁边。我设法做到这一点的方法是使用单例,目前它(似乎)工作得很好。现在的问题是,当我完全关闭并打开手机时,我不希望这些通知重置为零。如果有 7 个通知,我希望即使在设备重新启动后也有 7 个通知。为此,我显然需要一个持久的商店集成,我已经研究了一段时间。

到目前为止,我的裸单例代码是:

public class MyAppIndicator{
    public ApplicationIndicator _indicator; 
    public static MyAppIndicator _instance; 

    MyAppIndicator () {
        setupIndicator();   
    }

    public static MyAppIndicator getInstance() {
        if (_instance == null) {
            _instance = new MyAppIndicator ();
        }
        return(_instance);
    }

    public void setupIndicator() {

        //Setup notification 
        if (_indicator == null) {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
            _indicator = reg.getApplicationIndicator();

            if(_indicator == null) {
                ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("notificationsdemo_jde.png"));
                _indicator = reg.register(icon, false, true);  
                _indicator.setValue(0);
                _indicator.setVisible(false);
            }
        }
    }

    public void setVisible1(boolean visible, int count) {

        if (_indicator != null) {
            if (visible) {
                _indicator.setVisible(true);
                _indicator.setValue(count); //UserInterface.incrementCount()
            } else {
                _indicator.setVisible(false);
            }
        }
    }
}

我一直在使用黑莓教程来弄清楚如何实现持久存储:http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

现在,在我进一步讨论之前,我必须强调我非常对于java开发来说是新手,所以我的编码可能完全错误,但这就是我尝试做的事情:

public void setVisible1(boolean visible, int count) {

    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(count); //UserInterface.incrementCount()
            StoreInfo info = new StoreInfo(); 
            info.incElement();

            synchronized (persistentCount) { 
                //persistentCount.setContents(_data); 
                persistentCount.commit(); 
            } 


        } else {
            _indicator.setVisible(false);
        }
    }
}

static { 
    persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
    synchronized (persistentCount) { 
        if (persistentCount.getContents() == null) { 
            persistentCount.setContents(new Vector()); //don't know what to do with this?
            persistentCount.commit(); 
        } 
    } 
} 

private static final class StoreInfo implements Persistable{
    private int iconCount;
    public StoreInfo(){}

    public int getElement(){
        return (int)iconCount;
    }

    public void incElement(){
        iconCount++;             //persistently increment icon variable
    }

    public void resetElement(){
            iconCount=0;             //when user checks application
    }
}   

上面的代码不起作用,这是我所期望的,因为我在实现持久部分时遇到了麻烦。如果任何人对如何实现这一目标有任何想法或意见,任何帮助都会有所帮助。当然,提前致谢。

I currently have code to share a variable between two entry points in my application. The variable is the iconCount variable used to indicate how many notices the user has which is displayed on the home screen beside the icon. The way I've managed to do this is with a singleton and it (seems) to work fine at the moment. The issue is now that I do not want those notices to reset to zero when I completely turn off and turn on the phone. Should there be 7 notifications, I want there to be 7 notifications even after a device restart. For this I apparently need a persistent store integration which I've researched for a while.

So far my code for the bare singleton is:

public class MyAppIndicator{
    public ApplicationIndicator _indicator; 
    public static MyAppIndicator _instance; 

    MyAppIndicator () {
        setupIndicator();   
    }

    public static MyAppIndicator getInstance() {
        if (_instance == null) {
            _instance = new MyAppIndicator ();
        }
        return(_instance);
    }

    public void setupIndicator() {

        //Setup notification 
        if (_indicator == null) {
            ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
            _indicator = reg.getApplicationIndicator();

            if(_indicator == null) {
                ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("notificationsdemo_jde.png"));
                _indicator = reg.register(icon, false, true);  
                _indicator.setValue(0);
                _indicator.setVisible(false);
            }
        }
    }

    public void setVisible1(boolean visible, int count) {

        if (_indicator != null) {
            if (visible) {
                _indicator.setVisible(true);
                _indicator.setValue(count); //UserInterface.incrementCount()
            } else {
                _indicator.setVisible(false);
            }
        }
    }
}

I have been using the blackberry tutorial to figure out how to implement the persistable storage: http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

Now before I go any further I must stress I'm very new to java development so my coding might be completely wrong, but here is what I've tried to do:

public void setVisible1(boolean visible, int count) {

    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(count); //UserInterface.incrementCount()
            StoreInfo info = new StoreInfo(); 
            info.incElement();

            synchronized (persistentCount) { 
                //persistentCount.setContents(_data); 
                persistentCount.commit(); 
            } 


        } else {
            _indicator.setVisible(false);
        }
    }
}

static { 
    persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
    synchronized (persistentCount) { 
        if (persistentCount.getContents() == null) { 
            persistentCount.setContents(new Vector()); //don't know what to do with this?
            persistentCount.commit(); 
        } 
    } 
} 

private static final class StoreInfo implements Persistable{
    private int iconCount;
    public StoreInfo(){}

    public int getElement(){
        return (int)iconCount;
    }

    public void incElement(){
        iconCount++;             //persistently increment icon variable
    }

    public void resetElement(){
            iconCount=0;             //when user checks application
    }
}   

The code above doesn't work which I'd expect somehow because I'm having trouble implementing the persistent portion. If anyone has any idea or input on how to accomplish this any assistance would be helpful. And of course thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

第七度阳光i 2025-01-11 10:24:33

在示例中,它们有一个名为 _data 的变量,它保存 StoreInfo 类,因此首先您应该将 StoreInfo 保留在某个变量中。要做到这一点,请在静态初始值设定项中添加如下内容:

persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
synchronized (persistentCount) { 
    if (persistentCount.getContents() == null) { 
        persistentCount.setContents(new StoreInfo());
        persistentCount.commit(); 
    } 
}  
_data = (StoreInfo)persistentCount.getContents();

现在,当您想要更新它并将其保存到 PersistentStore 时,您可以使用类似的内容:

_data.incElement();
synchronized(persistentCount) {
    persistentCount.setContents(_data);
    persistentCount.commit();
}

假设您将只有一个 StoreInfo 实例,最好将将提交代码放入修饰符方法中,这样您就不会忘记将新值保存到 PersistentStore 中。

In the example they have a variable called _data that holds the StoreInfo class, so first of all you should be keeping the StoreInfo in some variable. To do this have something like the following in your static initializer:

persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
synchronized (persistentCount) { 
    if (persistentCount.getContents() == null) { 
        persistentCount.setContents(new StoreInfo());
        persistentCount.commit(); 
    } 
}  
_data = (StoreInfo)persistentCount.getContents();

Now when you want to update it and save to the PersistentStore you can have something like:

_data.incElement();
synchronized(persistentCount) {
    persistentCount.setContents(_data);
    persistentCount.commit();
}

Assuming you're going to only ever have one instance of StoreInfo it could be better to put the commit code into the modifier methods so you don't forget to save the new values to the PersistentStore.

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