Delete Persistent Object when app is Deleted in Blackberry

后端 未结 1 980
礼貌的吻别
礼貌的吻别 2021-01-21 10:55

I am using persistent object in blackberry to store config details specific to the app. Here is how I am implementing the class

public class Preferences implemen         


        
相关标签:
1条回答
  • 2021-01-21 11:31

    Create a custom hashtable class like this

    package com.myapp.items;
    
    
    import net.rim.device.api.util.Persistable;
    
    import java.util.*;
    
    public class MyAppHashtable extends Hashtable implements Persistable{
    
    }  
    

    And change your code to

    public class Preferences
    {
        private static  PersistentObject persistentObject = PersistentStore.getPersistentObject(0x2759d6ff72264bdbL);
        private static MyAppHashtable tbl = new MyAppHashtable ();
    
        public static void storeLoginToken(String token)
        {
            token = removeCharAt(token,0);
            token = removeCharAt(token,token.length()-1);
            tbl.put("token", token);
            persistentObject.setContents(tbl);
            persistentObject.commit();
        }
    
        public static String getLoginToken()
        {
            MyAppHashtable tbl = (MyAppHashtable )persistentObject.getContents();
            try
            {
                String token = tbl.get("token").toString();
                System.out.println("Token = "+token);
                return token;
            }
            catch(Exception e)
            {
                return null;
            }
    
        }
    }
    

    This is so that we adhere to the following info from RIM

    The BlackBerry persistence model

    When you use the BlackBerry persistence model, data is only deleted if the store contains data that belongs to the removed application.

    For example, if an application stores an object with a package called com.mycompany.application.storage and no other application on the BlackBerry smartphone makes reference to the package, the persistent store and the removed application are deleted.

    The same is true if the object is wrapped in a container such as a Vector. Even if only one of the elements of the Vector has a package name that is not used by other applications, the entire Vector is removed from the persistent store.

    Note: If the application does not store any objects with an identifying package structure, (for example, an application that stores java.util.Vector or javax.microedition.location.AddressInfo objects), the application should create and use a class that extends Vector in order to identify that Vector belongs to the given application. When you store this Vector, which is identified uniquely by its package, you guarantee that the data is removed from the persistent store when the application is removed.

    This info is from here

    0 讨论(0)
提交回复
热议问题