【问题标题】:Developing Persisent Store in Blackberry Java在 Blackberry Java 中开发持久存储
【发布时间】:2012-02-09 17:25:27
【问题描述】:

我目前有代码可以在我的应用程序的两个入口点之间共享一个变量。该变量是 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
    }
}   

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

【问题讨论】:

    标签: java eclipse blackberry persistence storage


    【解决方案1】:

    在示例中,他们有一个名为_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。

    【讨论】:

    • 您好,感谢您的回复,我已经使用您的示例工作了一段时间。我意识到我一直在处理的课程只是显示变量而不一定设置值,所以我已经做了几个小时没有进展。考虑到这是一个不同的课程,我认为为此打开另一个问题是合适的?不过谢谢你的帮助,它确实清楚了很多!
    猜你喜欢
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2014-05-05
    相关资源
    最近更新 更多