0

タスクは、BBDesktopManagerまたはその他の方法で永続オブジェクトをバックアップ/復元することです。主な目的は、デバイスのファームウェアアップデート間でデータを保持することです...

私は持っています:

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も実装しました(例の1つにあります)

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;
}}

エントリのpoingは次のとおりです。

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;
}}

ご覧のとおり、非常に簡単なはずです...

したがって、これらすべてをアプリケーションで実行し、永続オブジェクトに値を追加すると、それらは正しく追加され、デバイスのリセットなどで保存されます... Desktop Managerを実行してバックアップを作成すると、UserListがサイズとしてバックアップされているように見えますバックアップの量は、永続ストアに新しいデータを追加するとともに増加します。

しかし、BB 9300で「デバイスのワイプ」を実行し(そして永続ストアからのすべてのデータが期待どおりにクリアされます)、作成したばかりのバックアップファイルから復元を実行すると、アプリケーションで何も更新されず、永続ストアは空のようです。

いくつかの例では、代替エントリポイント「init」を追加していることがわかりましたが、EclipsePluginで説明されているようにすべてを調整することはできません。

バックアップファイルにデータを保存する方法と、バックアップから同じデータを取得してアプリケーションにロードする方法、またはDesktopManagerでイベントをログに記録する方法を教えてください。

4

1 に答える 1

1

誰かが同じ問題を経験している場合は、ワイプする前にデバイスを切断してみてください。奇妙ですが、役に立ちました:)

于 2011-10-31T08:30:07.497 に答える