BlackBerry应用程序之间通过persistant store传递数据

BlackBerry native程序之间,native程序和J2ME程序之间都可以通过BlackBerry Persistant Store API传递数据。

注:Persistant Store的好处是可以持久化。当然你也可以选择用RMS, BlackBerry Runtime Store传递参数,注意其各自的特点。

 

存参数:

datacontext.set("username", usernameField.getText().trim());
datacontext.commit();
取参数:
String username = (String) datacontext.get("username");

 

DataContext.java代码:

package util;

import java.util.Hashtable;

import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;

public class DataContext {
 private PersistentObject persistentObject;
 private Hashtable settingsTable;
 private long GUID=0xbbc7ce874824ab26L;

 public DataContext() {
  persistentObject = PersistentStore.getPersistentObject(GUID);
  synchronized(persistentObject) {
   settingsTable = (Hashtable)persistentObject.getContents();
      if (null == settingsTable) {
           settingsTable = new Hashtable();
           persistentObject.setContents(settingsTable);
           persistentObject.commit();
      }
  }
 }
  
 public void reset() {
  synchronized(persistentObject) {
       settingsTable = new Hashtable();
          persistentObject.setContents(settingsTable);
          persistentObject.commit();
  }
 }

 public Object get(String key) {
  return settingsTable.get(key);
 }
 public void set(String key, Object value) {
  settingsTable.put(key, value);
 }
 
 public void commit() {
  persistentObject.setContents(settingsTable);
  persistentObject.commit();
 }
}

你可能感兴趣的:(api,object,String,Class,BlackBerry,j2me)