gpt4 book ai didi

黑莓 - 应用程序设置保存/加载

转载 作者:行者123 更新时间:2023-12-02 09:00:43 26 4
gpt4 key购买 nike

我知道两种保存/加载应用程序设置的方法:

  • 使用PersistentStore
  • 使用文件系统(存储,因为 SDCard 是可选的)

我想知道您使用应用程序设置的做法是什么?

使用 PersistentStore 保存/加载应用程序设置

The persistent store provides a means for objects to persist across device resets. A persistent object consists of a key-value pair. When a persistent object is committed to the persistent store, that object's value is stored in flash memory via a deep copy. The value can then be retrieved at a later point in time via the key.

用于存储和检索设置的帮助器类示例:

class PSOptions {
private PersistentObject mStore;
private LongHashtableCollection mSettings;
private long KEY_URL = 0;
private long KEY_ENCRYPT = 1;
private long KEY_REFRESH_PERIOD = 2;

public PSOptions() {
// "AppSettings" = 0x71f1f00b95850cfeL
mStore = PersistentStore.getPersistentObject(0x71f1f00b95850cfeL);
}

public String getUrl() {
Object result = get(KEY_URL);
return (null != result) ? (String) result : null;
}

public void setUrl(String url) {
set(KEY_URL, url);
}

public boolean getEncrypt() {
Object result = get(KEY_ENCRYPT);
return (null != result) ? ((Boolean) result).booleanValue() : false;
}

public void setEncrypt(boolean encrypt) {
set(KEY_ENCRYPT, new Boolean(encrypt));
}

public int getRefreshPeriod() {
Object result = get(KEY_REFRESH_PERIOD);
return (null != result) ? ((Integer) result).intValue() : -1;
}

public void setRefreshRate(int refreshRate) {
set(KEY_REFRESH_PERIOD, new Integer(refreshRate));
}

private void set(long key, Object value) {
synchronized (mStore) {
mSettings = (LongHashtableCollection) mStore.getContents();
if (null == mSettings) {
mSettings = new LongHashtableCollection();
}
mSettings.put(key, value);
mStore.setContents(mSettings);
mStore.commit();
}
}

private Object get(long key) {
synchronized (mStore) {
mSettings = (LongHashtableCollection) mStore.getContents();
if (null != mSettings && mSettings.size() != 0) {
return mSettings.get(key);
} else {
return null;
}
}
}
}

sample app screen http://img182.imageshack.us/img182/6348/appsettings.png

使用示例:

class Scr extends MainScreen implements FieldChangeListener {
PSOptions mOptions = new PSOptions();

BasicEditField mUrl = new BasicEditField("Url:",
"http://stackoverflow.com/");
CheckboxField mEncrypt = new CheckboxField("Enable encrypt", false);
GaugeField mRefresh = new GaugeField("Refresh period", 1, 60 * 10, 10,
GaugeField.EDITABLE|FOCUSABLE);
ButtonField mLoad = new ButtonField("Load settings",
ButtonField.CONSUME_CLICK);
ButtonField mSave = new ButtonField("Save settings",
ButtonField.CONSUME_CLICK);

public Scr() {
add(mUrl);
mUrl.setChangeListener(this);
add(mEncrypt);
mEncrypt.setChangeListener(this);
add(mRefresh);
mRefresh.setChangeListener(this);
HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH);
add(hfm);
hfm.add(mLoad);
mLoad.setChangeListener(this);
hfm.add(mSave);
mSave.setChangeListener(this);
loadSettings();
}

public void fieldChanged(Field field, int context) {
if (field == mLoad) {
loadSettings();
} else if (field == mSave) {
saveSettings();
}
}

private void saveSettings() {
mOptions.setUrl(mUrl.getText());
mOptions.setEncrypt(mEncrypt.getChecked());
mOptions.setRefreshRate(mRefresh.getValue());
}

private void loadSettings() {
mUrl.setText(mOptions.getUrl());
mEncrypt.setChecked(mOptions.getEncrypt());
mRefresh.setValue(mOptions.getRefreshPeriod());
}
}

最佳答案

第三个选项:使用 RMS 进行简单的应用程序设置。

虽然您建议的持久存储听起来不错,但它与任何其他 java 手机都不兼容,因此当您必须移植应用程序时,您必须重新创建这部分

我在某处读到,只有在图片或视频的情况下才应该在文件系统本身上创建文件,因此基本上用户也可以以另一种方式查看内容。

关于黑莓 - 应用程序设置保存/加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1476994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com