我目前有代碼在我的應用程序中的兩個入口點之間共享一個變量。該變量是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電子不工作,我期望某種方式,因爲我在實施持久部分有困難。如果任何人有任何想法或意見如何完成此任何援助將是有益的。當然,在此先感謝。
嘿謝謝你的回覆,我一直在努力處理你的例子。我意識到我一直在處理的課程只是顯示變量,並不一定會設置值,所以我已經在這個幾個小時沒有進展。考慮到這是一個不同的課程,我認爲爲此打開另一個問題是適當的?但是,謝謝你的幫助,它清理了很多! – user1152440 2012-02-09 17:04:58