0

我正在开发一个 BlackBerry 应用程序StringbooleanPersistent Store. 我知道当从手机中删除/卸载应用程序时,字符串和布尔值不会从 Persistent 中删除。我也知道,为了删除这些值,我需要将其存储在“项目”特定类中。我一直在努力解决这个问题,所以想要一个临时的工作。我正在保存一个布尔值,以确定应用程序应该加载哪个屏幕。

PersistentStoreHelper.persistentHashtable.put("flagged",Boolean.TRUE); 

if (PersistentStoreHelper.persistentHashtable.containsKey("flagged")) 
        {
           boolean booleanVal = ((Boolean) PersistentStoreHelper.persistentHashtable.get("flagged")).booleanValue();
           if (booleanVal==true)
           {
              pushScreen(new MyScreen());
           }
        }
        else
        {
            pushScreen(new MyScreen(false));
        }

是否可以将此布尔值存储为对象,以便在卸载/删除应用程序时将其删除。如果我错过了什么,请提供帮助并发表评论。

4

1 回答 1

2

再次,我建议您在线更改PersistentStoreHelper为此版本

当您的应用程序被卸载时,您当然可以从持久存储中删除BooleanString删除值,但它们需要位于只能存在于您的应用程序中的对象内。

例如:

 PersistentStoreHelper store = PersistentStoreHelper.getInstance();
 store.put("flagged", Boolean.TRUE);
 // commit will save changes to the `flagged` variable
 store.commit();

然后稍后使用以下方法检索它:

 PersistentStoreHelper store = PersistentStoreHelper.getInstance();
 boolean isFlagged = ((Boolean)store.get("flagged")).booleanValue();

使这项工作的关键在我的PersistentStoreHelper类中,它将所有内容保存在我/您的应用程序()独有的子类中。您需要将您的或对象存储在该应用程序唯一的子类中,而不是普通的.HashtableMyAppsHashtableStringBooleanHashtablejava.util.Hashtable

再一次,请让自己轻松一点,并使用我发布的代码。

Note: also, you probably know this, but you may need to reboot your device to see the app, and its persistent store data, fully deleted.

Update

If you had changed the original PersistentStoreHelper class that I had posted online, because you wanted access to the containsKey() method, or other methods in the Hashtable class, you can solve that problem by simply adding code like this:

public boolean containsKey(String key) {
    return persistentHashtable.containsKey(key);
}

to the PeristentStoreHelper class. Please don't make persistentHashtable a public static member. As you need to use more Hashtable methods, just add wrappers for them, like I show above with containsKey(). Of course, you can achieve the same thing as containsKey() by simply using this code:

 boolean containsFlagged = (store.get("flagged") != null);

Update 2

If you get stuck with old persistent data, that you need to clean out, you can modify PersistentStoreHelper to detect and correct the situation, like this (hattip to @adwiv for suggestions):

   private PersistentStoreHelper() {
      persistentObject = PersistentStore.getPersistentObject(KEY);

      Object contents = persistentObject.getContents();
      if (contents instanceof MyAppsHashtable) {
         persistentHashtable = (MyAppsHashtable)contents;
      } else {
         // store might be empty, or contents could be the wrong type
         persistentHashtable = new MyAppsHashtable();
         persistentObject.setContents(persistentHashtable);
         persistentObject.commit();
      }
   }
于 2013-04-09T08:11:46.117 回答