Java 给定运行时存储中的持久存储

发布于 2025-01-03 17:31:32 字数 1940 浏览 4 评论 0 原文

我正在尝试创建一个持久且共享的变量,该变量将跟踪黑莓应用程序中用户可用的通知数量。该号码显示在主屏幕上,即使在设备关闭后也应保留,直到他们自己检查应用程序,然后重置该号码。我一直在使用单例在后台进程和 UI 应用程序本身之间共享变量,如下:

import net.rim.device.api.system.RuntimeStore;

public class IconManager {
    private static IconManager _instance;
    private static final long GUID = 0xab4dd61c5d004c18L;
    private int iconCount;

    // constructor
    private IconManager() {
        iconCount = 0;
    }

    public static IconManager getInstance() {
        if (_instance == null) {
            _instance = (IconManager) RuntimeStore.getRuntimeStore().get(GUID);
            if (_instance == null) {
                IconManager singleton = new IconManager();

                RuntimeStore.getRuntimeStore().put(GUID, singleton); 
                _instance = singleton;
            }
        }
        return _instance;
    }

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
    }
}

我主要使用此站点来尝试找出持久存储部分: http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

考虑到上述运行时存储,是否有替代方案来实现持久存储?我最初考虑使用黑莓示例中的代码,但我对如何执行此操作感到困惑。在另一个线程中,用户 mparizeau 写了以下内容:

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();
}

可以在上面的代码中以某种方式使用它吗?我对 java 和 BB 开发非常陌生,因此我们将不胜感激。

I am trying to create a persistent and shared variable that will keep track of the number of notifications available to the user in a Blackberry app. This number is showed on the home screen and should be kept even after the device is turned off until they check the application themselves, then the number is reset. I have been using a singleton to share the variable between the background process and the UI app itself below:

import net.rim.device.api.system.RuntimeStore;

public class IconManager {
    private static IconManager _instance;
    private static final long GUID = 0xab4dd61c5d004c18L;
    private int iconCount;

    // constructor
    private IconManager() {
        iconCount = 0;
    }

    public static IconManager getInstance() {
        if (_instance == null) {
            _instance = (IconManager) RuntimeStore.getRuntimeStore().get(GUID);
            if (_instance == null) {
                IconManager singleton = new IconManager();

                RuntimeStore.getRuntimeStore().put(GUID, singleton); 
                _instance = singleton;
            }
        }
        return _instance;
    }

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
    }
}

I have been mainly using this site to try to figure out the Persistent store portion: http://supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747

Is there an alternative to implement the persistent store given the above runtimestore? I was originally thinking of using code from the Blackberry example, but I'm confused on how to do this. From another thread user mparizeau wrote the following:

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();
}

Could this be used in the above code somehow? I am extremely new to java and BB development so any help would be appreciated.

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

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

发布评论

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

评论(2

浪漫之都 2025-01-10 17:31:32

我认为您不想使用 RunTimeStore,因为您希望即使在设备关闭后信息也能保留。来自此页面

运行时存储不是持久的。当您重新启动 BlackBerry 时
设备,运行时存储中的数据被清除。

尝试这样的事情:

public class IconManager {
    private static IconManager _instance;
    private final long GUID = 0xab4dd61c5d004c18L;
    private PersistentObject store;
    private int iconCount;

    private IconManager() {
        store = PersistentStore.getPersistentObject(GUID);
        synchronized(store) {
            if(store.getContents() == null) {
                store.setContents(new Integer(0));
                store.commit();
            }
        }
        iconCount = ((Integer)store.getContents()).intValue();
    }

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

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
        synchronized(store) {
            store.setContents(new Integer(iconCount));
            store.commit();
        }
    }
}

I don't think you want to use RunTimeStore, since you want to the information to persist even after the device is turned off. From this page

The runtime store is not persistent. When you restart the BlackBerry
device, the data in the runtime store clears.

Try something like this:

public class IconManager {
    private static IconManager _instance;
    private final long GUID = 0xab4dd61c5d004c18L;
    private PersistentObject store;
    private int iconCount;

    private IconManager() {
        store = PersistentStore.getPersistentObject(GUID);
        synchronized(store) {
            if(store.getContents() == null) {
                store.setContents(new Integer(0));
                store.commit();
            }
        }
        iconCount = ((Integer)store.getContents()).intValue();
    }

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

    public int getCount() {             
        return iconCount;
    }

    public void setCount(int count) {      
        iconCount = count;
        synchronized(store) {
            store.setContents(new Integer(iconCount));
            store.commit();
        }
    }
}
给妤﹃绝世温柔 2025-01-10 17:31:32

Blackberry OS 5 及更新版本内置了 SQLite。您可以使用它来代替持久存储。 (它有一个类似 jdbc 的 API)。 BBOS 5 已经发布有一段时间了。

Blackberry OS's 5 and newer have SQLite built in. You can use that instead of the persistent store. (It has a jdbc-like API). BBOS 5 has been out for quite some time now.

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