备份/恢复net.rim.device.api.util.Persitable对象与desktopmanager



任务是用BB桌面管理器或以任何其他方式备份/恢复可持久对象。主要目的是在设备固件更新之间保持数据…

:

public final class UserList implements Persistable {
//The persistable objects.
private Hashtable fData;
//Initialize the class with empty values.
public UserList() {
    fData = new Hashtable();
}
//Initialize the class with the specified values.
public UserList(Hashtable p) {
    fData = p;
}
public Hashtable getData() {
    return fData;
}}

我也实现了SyncItem(如在一个例子中发现)

public final class UserListSync extends SyncItem {
private static UserList fList;
private static final int FIELDTAG_NAME = 1;
private static final int FIELDTAG_AGE = 2;
private static PersistentObject store;
static {
    store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}
public UserListSync() {
}
public String getSyncName() {
    return "Sync Item Sample";
}
public String getSyncName(Locale locale) {
    return null;
}
public int getSyncVersion() {
    return 1;
}
public boolean getSyncData(DataBuffer db, int version) {
    boolean retVal = true;
    synchronized (store) {
        if (store.getContents() != null) {
            fList = (UserList)store.getContents();              
        }
    }
    try {
        Enumeration e = fList.getData().keys();
        while (e.hasMoreElements()) {
            String key = (String) e.nextElement();
            String value = (String) fList.getData().get(key); 
            //Write the name.
            db.writeShort(key.length() + 1);
            db.writeByte(FIELDTAG_NAME);
            db.write(key.getBytes());
            db.writeByte(0);
            //Write the age.
            db.writeShort(value.length() + 1);
            db.writeByte(FIELDTAG_AGE);
            db.write(value.getBytes());
            db.writeByte(0);
        }           
    } catch (Exception e) {
        retVal = false;
    }
    return retVal;
}
//Interprets and stores the data sent from the Desktop Manager.
public boolean setSyncData(DataBuffer db, int version) {
    int length;
    Hashtable table = new Hashtable();
    Vector keys = new Vector();
    Vector values = new Vector();
    boolean retVal = true;
    try {
        //Read until the end of the Databuffer.
        while (db.available() > 0) {
            //Read the length of the data.
            length = db.readShort();
            //Set the byte array to the length of the data.
            byte[] bytes = new byte[length];
            //Determine the type of data to be read (name or age).
            switch (db.readByte()) {
                case FIELDTAG_NAME:
                    db.readFully(bytes);
                    keys.addElement(new String(bytes).trim());
                    break;
                case FIELDTAG_AGE:
                    db.readFully(bytes);
                    values.addElement(new String(bytes).trim());
                    break;
            }
        }
    } catch (Exception e) {
        retVal = false;
    }
    for (int i = 0; i < keys.size(); i++) {
        table.put(keys.elementAt(i), values.elementAt(i));
    }
    try {
        //Store the new data in the persistent store object.
        fList = new UserList(table);
        store.setContents(fList);
        store.commit();
    } catch (Exception e) {
        retVal = false;
    }
    return retVal;
}}

入口点操作如下:

public class SyncItemSample extends UiApplication {
private static PersistentObject store;
private static UserList userList;
static {
    store = PersistentStore.getPersistentObject(0x3167239af4aa40fL);
}
public static void main(String[] args) {
    SyncItemSample app = new SyncItemSample();
    app.enterEventDispatcher();
}
public SyncItemSample() {
    UserListScreen userListScreen;
    //Check to see if the store exists on the BlackBerry.
    synchronized (store) {
        if (store.getContents() == null) {
            //Store does not exist, create it with default values
            userList = new UserList();
            store.setContents(userList);
            store.commit();
        } else {
            //Store exists, retrieve data from store.
            userList = (UserList)store.getContents();               
        }
    }
    //Create and push the UserListScreen.
    userListScreen = new UserListScreen(userList);
    pushScreen(userListScreen);
}}

下面是screen的实现:

public final class UserListScreen extends MainScreen {
Vector fLabels = new Vector();
Vector fValues = new Vector();
VerticalFieldManager leftColumn = new VerticalFieldManager();
VerticalFieldManager rightColumn = new VerticalFieldManager();
UserList fList;
public UserListScreen(UserList list) {
    super();
    fList = list;
    //Create a horizontal field manager to hold the two vertical field
    //managers to display the names and ages in two columns.
    VerticalFieldManager inputManager = new VerticalFieldManager();
    HorizontalFieldManager backGround = new HorizontalFieldManager();
    //Array of fields to display the names and ages.
    LabelField title = new LabelField("User List",
    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
    setTitle(title);
    final TextField fld1 = new TextField(TextField.NO_NEWLINE);
    fld1.setLabel("input label");
    inputManager.add(fld1);
    final TextField fld2 = new TextField(TextField.NO_NEWLINE);
    fld2.setLabel("input value");
    inputManager.add(fld2);
    final ButtonField fld3 = new ButtonField();
    fld3.setChangeListener(new FieldChangeListener() {          
        public void fieldChanged(Field field, int context) {
            fList.getData().put(fld1.getText().trim(), fld2.getText().trim());
            refresh();
        }
    });
    fld3.setLabel("add");
    inputManager.add(fld3);     
    add(inputManager);
    //Add the column titles and a blank field to create a space.
    LabelField leftTitle = new LabelField("label ");
    leftColumn.add(leftTitle);
    LabelField rightTitle = new LabelField("value");
    rightColumn.add(rightTitle);
    refresh();
    //Add the two vertical columns to the horizontal field manager.
    backGround.add(leftColumn);
    backGround.add(rightColumn);
    //Add the horizontal field manager to the screen.
    add(backGround);
}
private void refresh() {
    leftColumn.deleteAll();
    rightColumn.deleteAll();
    fLabels.removeAllElements();
    fValues.removeAllElements();
    //Populate and add the name and age fields.
    Enumeration e = fList.getData().keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        String value = (String) fList.getData().get(key); 
        final LabelField tmp1 = new LabelField(key);
        final LabelField tmp2 = new LabelField(value);
        leftColumn.add(tmp1);
        rightColumn.add(tmp2);
        fLabels.addElement(tmp1);
        fValues.addElement(tmp2);
    }
}
public boolean onClose() {
    System.exit(0);
    return true;
}}

正如你所看到的,它应该非常容易…

所以所有这些我运行应用程序,添加值到持久对象,它们被正确添加,在设备重置期间存储等等…当我运行桌面管理器并进行备份时,似乎UserList已备份,因为备份的大小随着向持久存储中添加新数据而增长。

但是当我在BB 9300上运行"擦设备"时(并且持久化存储的所有数据都按预期清除),然后从刚刚制作的备份文件运行还原-应用程序中没有任何更新,持久化存储似乎是空的。

在一些例子中,我发现添加替代入口点"init",但我不能像我的EclipsePlugin

所描述的那样调整一切

你能告诉我如何在备份文件中存储数据,如何从备份中检索相同的数据并将其加载回应用程序,或者如何使用桌面管理器记录任何事件吗?

如果有人遇到了同样的问题,您可以在擦除设备之前尝试断开连接。这是奇怪的,但它帮助:)

相关内容

最新更新