2012-02-08 145 views
0

我目前有代码在我的应用程序中的两个入口点之间共享一个变量。该变量是iconCount变量,用于指示用户在主屏幕上显示的图标旁边显示的通知数量。我设法做到这一点的方式是用一个单身人士,现在看起来似乎很好。现在的问题是,当我完全关闭并打开手机时,我不希望这些通知重置为零。如果有7个通知,那么即使在设备重新启动后,我也希望有7个通知。为此,我显然需要一个持续的商店整合,我已经研究了一段时间。在黑莓Java中开发Persisent商店

到目前为止,我对裸单码:

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 
    } 
} 

代码abov电子不工作,我期望某种方式,因为我在实施持久部分有困难。如果任何人有任何想法或意见如何完成此任何援助将是有益的。当然,在此先感谢。

回答

0

在这个例子中,他们有一个名为_data的变量,它包含StoreInfo类,所以首先你应该保留StoreInfo的一些变量。要做到这一点有一些像在静态初始化如下:当你想更新并保存到PersistentStore你可以像

persistentCount = PersistentStore.getPersistentObject(0xdec6a67096f833cL); 
synchronized (persistentCount) { 
    if (persistentCount.getContents() == null) { 
     persistentCount.setContents(new StoreInfo()); 
     persistentCount.commit(); 
    } 
} 
_data = (StoreInfo)persistentCount.getContents(); 

现在:

_data.incElement(); 
synchronized(persistentCount) { 
    persistentCount.setContents(_data); 
    persistentCount.commit(); 
} 

假设你要去为了只有一个StoreInfo实例,最好将提交代码放入修饰符方法中,这样您就不会忘记将新值保存到PersistentStore中。

+0

嘿谢谢你的回复,我一直在努力处理你的例子。我意识到我一直在处理的课程只是显示变量,并不一定会设置值,所以我已经在这个几个小时没有进展。考虑到这是一个不同的课程,我认为为此打开另一个问题是适当的?但是,谢谢你的帮助,它清理了很多! – user1152440 2012-02-09 17:04:58