2012-02-09 67 views
0

我正在尝试创建一个持久和共享变量,以便跟踪黑莓应用程序中用户可用的通知数。此号码显示在主屏幕上,即使设备关闭后仍应保留,直到他们自己检查应用程序,然后重置号码。我一直在使用一个单独共享后台进程和UI应用程序本身下面的变量:Java中的永久存储区给定的运行时间存储

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开发非常新,所以任何帮助,将不胜感激。

+0

你究竟需要什么?明确指出.. – 2012-02-10 03:58:32

回答

1

我不认为你想使用RunTimeStore,因为你想要的信息,即使在设备关闭后仍然存在。从this page

运行时存储不是持久的。当您重新启动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(); 
     } 
    } 
} 
+0

非常感谢您的帮助!代码完美编译,但只有当我将iconCount声明更改为静态时。现在,在将静态声明放在该声明不会使代码正常工作之前,我遇到了一个问题,并且不幸的是,iconCount变量在此情况下也未保存。我认为它可能与iconCount被更改为静态以便编译有关。你知道这个方法吗? – user1152440 2012-02-10 14:50:09

+0

从上面的代码也不会单身需要存储?因为我认为单例是保持两个事件之间变量同步的东西,所以不会破坏事件1和事件2之间的共同变量之间的关系? – user1152440 2012-02-10 15:13:52

+0

糟糕,在这种情况下,它实际上应该是静态的。然而,我刚刚意识到,既然你使用它作为一个单例,你实际上不需要它是静态的,你应该能够将静态初始化器中的东西移动到构造器中。我会更新代码以反映这一点。 – mparizeau 2012-02-11 01:31:45

0

黑莓OS的5和更新已建成的SQLite中可以使用的,与其持久性存储。 (它有一个类似jdbc的API)。 BBOS 5已经出现了很长时间了。