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.
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
Try something like this: