【发布时间】: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