在blackberryjava中开发应用商店
我目前有代码在我的应用程序的两个入口点之间共享一个变量。变量是iconCount变量,用于指示用户在主屏幕上图标旁边显示的通知数量。我设法做到这一点的方法是用一个单身汉,目前它(似乎)工作得很好。现在的问题是,当我完全关闭和打开手机时,我不希望这些通知重置为零。如果有7个通知,我希望即使在设备重新启动后也有7个通知。为此,我显然需要一个持久的存储集成,我已经研究了一段时间 到目前为止,我的裸单例代码是:在blackberryjava中开发应用商店,java,eclipse,blackberry,persistence,storage,Java,Eclipse,Blackberry,Persistence,Storage,我目前有代码在我的应用程序的两个入口点之间共享一个变量。变量是iconCount变量,用于指示用户在主屏幕上图标旁边显示的通知数量。我设法做到这一点的方法是用一个单身汉,目前它(似乎)工作得很好。现在的问题是,当我完全关闭和打开手机时,我不希望这些通知重置为零。如果有7个通知,我希望即使在设备重新启动后也有7个通知。为此,我显然需要一个持久的存储集成,我已经研究了一段时间 到目前为止,我的裸单例代码是: public class MyAppIndicator{ public Applic
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);
}
}
}
}
我一直在使用blackberry教程了解如何实现持久存储:
现在,在进一步讨论之前,我必须强调,我对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
}
}
上面的代码无法正常工作,这是我所期望的,因为我在实现持久部分时遇到了问题。如果有人对如何实现这一目标有任何想法或意见,任何帮助都会很有帮助。当然还要提前感谢。在这个示例中,他们有一个名为
\u data
的变量,它保存StoreInfo
类,因此首先应该将StoreInfo
保存在某个变量中。要做到这一点,请在静态初始值设定项中执行以下操作:
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();
}
假设您只有一个StoreInfo实例,最好将提交代码放入修饰符方法中,这样您就不会忘记将新值保存到PersistentStore。嘿,谢谢您的回复,我已经使用您的示例一段时间了。我意识到我正在处理的类只是显示变量,不一定设置值,所以我在这方面已经做了几个小时,没有任何进展。考虑到这是一门不同的课,我认为为这门课提出另一个问题是合适的?但是谢谢你的帮助,天气确实转晴了很多!