[英]Persistent Store in Java Given Runtime Store
我正在嘗試創建一個持久性和共享變量,該變量將跟蹤Blackberry應用程序中用戶可用的通知數量。 此號碼顯示在主屏幕上,即使在關閉設備電源后也應保留此號碼,直到他們自己檢查應用程序,然后重置該號碼。 我一直在使用單例在下面的后台進程和UI應用程序本身之間共享變量:
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;
}
}
我主要使用此站點來嘗試找到持久性存儲部分: http : //supportforums.blackberry.com/t5/Java-Development/Storing-persistent-data/ta-p/442747
給定上述運行時存儲區,是否可以實現持久存儲區? 我本來是想使用Blackberry示例中的代碼,但是我對如何執行此操作感到困惑。 mparizeau用戶從另一個線程寫了以下內容:
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();
}
可以在上面的代碼中以某種方式使用它嗎? 我對Java和BB開發非常陌生,因此可以提供任何幫助。
我不希望使用RunTimeStore,因為即使在設備關閉后,您也希望信息能夠保留。 從這個頁面
運行時存儲不是持久性的。 重新啟動BlackBerry設備時,運行時存儲中的數據將清除。
嘗試這樣的事情:
public class IconManager {
private static IconManager _instance;
private final long GUID = 0xab4dd61c5d004c18L;
private PersistentObject store;
private int iconCount;
private IconManager() {
store = PersistentStore.getPersistentObject(GUID);
synchronized(store) {
if(store.getContents() == null) {
store.setContents(new Integer(0));
store.commit();
}
}
iconCount = ((Integer)store.getContents()).intValue();
}
public static IconManager getInstance() {
if (_instance == null) {
_instance = new IconManager();
}
return _instance;
}
public int getCount() {
return iconCount;
}
public void setCount(int count) {
iconCount = count;
synchronized(store) {
store.setContents(new Integer(iconCount));
store.commit();
}
}
}
Blackberry OS 5和更高版本內置了SQLite。您可以使用它代替持久性存儲。 (它具有類似於jdbc的API)。 BBOS 5已經問世了一段時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.