Java RMS存储值在J2ME emulator中不是永久可用的
我有在J2ME中使用RMS存储值的代码。它在emulator上运行良好。所以,我的第一个问题是Java RMS存储值在J2ME emulator中不是永久可用的,java,netbeans,java-me,midp,rms,Java,Netbeans,Java Me,Midp,Rms,我有在J2ME中使用RMS存储值的代码。它在emulator上运行良好。所以,我的第一个问题是 当我重新启动模拟器时,所有存储的值都会被删除 存储值显示在emulator中,但不显示在我正在测试应用程序的移动设备中 我正在使用NetBeans开发我的J2ME应用程序 ==更新==== /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
public class TryNew extends MIDlet implements CommandListener, ItemCommandListener {
private RecordStore record;
private StringItem registered;
static final String REC_STORE = "SORT";
//Button existUser;
Display display = null;
private Ticker ticker;
Form form = null;
Form form1 = null;
TextField tb, tb1, tb2, tb3;
ChoiceGroup operator = null;
String str = null;
Command backCommand = new Command("Back", Command.BACK, 0);
Command loginCommand = new Command("Login", Command.OK, 2);
Command saveCommand = new Command("Save New", Command.OK, 1);
Command sendCommand = new Command("Send", Command.OK, 2);
Command selectCommand = new Command("Select", Command.OK, 0);
Command exitCommand = new Command("Exit", Command.STOP, 3);
private ValidateLogin ValidateLogin;
public TryNew() {
}
public void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
form = new Form("Login");
registered = new StringItem("", "Registered ?", StringItem.BUTTON);
form1 = new Form("Home");
tb = new TextField("Login Id: ", "", 10, TextField.PHONENUMBER);//TextField.PHONENUMBER
tb1 = new TextField("Password: ", "", 30, TextField.PASSWORD);
operator = new ChoiceGroup("Select Website", Choice.POPUP, new String[]{"Game", "Joke", "SMS"}, null);
form.append(tb);
form.append(tb1);
form.append(operator);
form.append(registered);
registered.setDefaultCommand(selectCommand);
registered.setItemCommandListener(this);
form.addCommand(saveCommand);
ticker = new Ticker("Welcome Screen");
form.addCommand(loginCommand);
form.addCommand(selectCommand);
form.addCommand(exitCommand);
// existUser = new StringItem(null, "Registered ?");
// form.append(existUser);
form.setCommandListener(this);
form1.addCommand(exitCommand);
form1.addCommand(sendCommand);
form1.setCommandListener(this);
form.setTicker(ticker);
display.setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
void showMessage(String message, Displayable displayable) {
Alert alert = new Alert("");
alert.setTitle("Error");
alert.setString(message);
alert.setType(AlertType.ERROR);
alert.setTimeout(5000);
display.setCurrent(alert, displayable);
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == backCommand) {
display.setCurrent(form);
} else if (c == loginCommand) {
ValidateLogin = new ValidateLogin(this);
ValidateLogin.start();
ValidateLogin.validateLogin(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
} else if (c == saveCommand) {
openRecord();
writeRecord(tb.getString(), tb1.getString(), operator.getString(operator.getSelectedIndex()));
closeRecord();
showAlert("Login Credential Saved Successfully !!");
}
}
////==============================================================================/////
/// Record Management
public void openRecord() {
try {
record = RecordStore.openRecordStore(REC_STORE, true);
} catch (Exception e) {
db(e.toString());
}
}
public void closeRecord() {
try {
record.closeRecordStore();
} catch (Exception e) {
db(e.toString());
}
}
public void deleteRecord() {
if (RecordStore.listRecordStores() != null) {
try {
RecordStore.deleteRecordStore(REC_STORE);
} catch (Exception e) {
db(e.toString());
}
}
}
public void writeRecord(String login_id, String pwd, String operator_name) {
String credential = login_id + "," + pwd + "," + operator_name;
byte[] rec = credential.getBytes();
try {
if (login_id.length() > 10 || login_id.length() < 10) {
showAlert("Please Enter valid Login Id");
} else if (pwd.length() < 1) {
showAlert("Please Password !!");
} else {
record.addRecord(rec, 0, rec.length);
}
} catch (Exception e) {
db(e.toString());
}
}
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
public void readRecord() {
try {
if (record.getNumRecords() > 0) {
Comparator comp = new Comparator();
RecordEnumeration re = record.enumerateRecords(null, comp, false);
while (re.hasNextElement()) {
String str = new String(re.nextRecord());
showAlert(str);
}
}
} catch (Exception e) {
db(e.toString());
}
}
private void db(String error) {
System.err.println("Exception: " + error);
}
public void commandAction(Command c, Item item) {
if (c == selectCommand && item == registered) {
openRecord();
readRecord();
closeRecord();
}
}
class Comparator implements RecordComparator {
public int compare(byte[] rec1, byte[] rec2) {
String str1 = new String(rec1);
String str2 = new String(rec2);
int result = str1.compareTo(str2);
if (result == 0) {
return RecordComparator.EQUIVALENT;
} else if (result < 0) {
return RecordComparator.PRECEDES;
} else {
return RecordComparator.FOLLOWS;
}
}
}
class ValidateLogin implements Runnable {
TryNew midlet;
private Display display;
String login_id;
String pwd;
String operator_name;
public ValidateLogin(TryNew midlet) {
this.midlet = midlet;
display = Display.getDisplay(midlet);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
if (login_id.length() > 10 || login_id.length() < 10) {
showAlert("Please Enter valid Login Id");
} else if (pwd.length() < 1) {
showAlert("Please Password !!");
} else {
showHome();
}
}
/* This method takes input from user like text and pass
to servlet */
public void validateLogin(String login_id, String pwd, String operator_name) {
this.login_id = login_id;
this.pwd = pwd;
this.operator_name = operator_name;
}
/* Display Error On screen*/
private void showAlert(String err) {
Alert a = new Alert("");
a.setString(err);
a.setTimeout(Alert.FOREVER);
display.setCurrent(a);
}
private void showHome() {
tb2 = new TextField("To: ", "", 30, TextField.PHONENUMBER);
tb3 = new TextField("Message: ", "", 300, TextField.ANY);
form1.append(tb2);
form1.append(tb3);
form1.addCommand(loginCommand);
//display.setCurrent(tb3);
display.setCurrent(form1);
}
};
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
导入javax.microedition.lcdui.*;
导入javax.microedition.midlet.*;
导入javax.microedition.rms.*;
公共类TryNew扩展MIDlet实现CommandListener、ItemCommandListener{
私有记录存储记录;
私人物品登记;
静态最终字符串REC_STORE=“SORT”;
//按钮输出器;
显示=空;
私人售票机;
Form=null;
form1=空;
文本字段tb、tb1、tb2、tb3;
ChoiceGroup运算符=null;
字符串str=null;
命令backCommand=新命令(“Back”,Command.Back,0);
命令loginCommand=新命令(“Login”,Command.OK,2);
命令saveCommand=new命令(“savenew”,Command.OK,1);
Command sendCommand=新命令(“发送”,Command.OK,2);
命令选择命令=新命令(“选择”,Command.OK,0);
命令exitCommand=新命令(“退出”,Command.STOP,3);
私有ValidateLogin ValidateLogin;
公共TryNew(){
}
public void startApp()引发MIDletStateChangeException{
display=display.getDisplay(这个);
表单=新表单(“登录”);
已注册=新StringItem(“,“已注册?”,StringItem.BUTTON);
表格1=新表格(“主页”);
tb=new TextField(“登录Id:”,“”,10,TextField.PHONENUMBER);//TextField.PHONENUMBER
tb1=新文本字段(“密码:”,“”,30,TextField.Password);
operator=new ChoiceGroup(“选择网站”,Choice.POPUP,新字符串[]{“游戏”,“笑话”,“短信”},空);
附加表格(tb);
附加表格(tb1);
表单追加(运算符);
表格。附加(注册);
registered.setDefaultCommand(selectCommand);
registered.setItemCommandListener(此);
form.addCommand(saveCommand);
股票代码=新股票代码(“欢迎屏幕”);
form.addCommand(loginCommand);
form.addCommand(selectCommand);
form.addCommand(exitCommand);
//existUser=newstringitem(null,“已注册?”);
//附加表格(existUser);
form.setCommandListener(this);
form1.addCommand(exitCommand);
表格1.addCommand(sendCommand);
form1.setCommandListener(此);
表格.setTicker(ticker);
显示。设置当前(表格);
}
公共无效pauseApp(){
}
公共应用程序(布尔无条件){
}
void showMessage(字符串消息,可显示){
警报警报=新警报(“”);
警报。设置标题(“错误”);
警报.设置字符串(消息);
alert.setType(AlertType.ERROR);
警报。设置超时(5000);
显示。设置当前(警报,可显示);
}
公共无效命令操作(命令c,可显示d){
if(c==exitCommand){
销毁应用程序(真);
通知销毁();
}else if(c==backCommand){
显示。设置当前(表格);
}else if(c==loginCommand){
ValidateLogin=新的ValidateLogin(此);
ValidateLogin.start();
ValidateLogin.ValidateLogin(tb.getString()、tb1.getString()、operator.getString(operator.getSelectedIndex());
}else if(c==saveCommand){
openRecord();
writeRecord(tb.getString(),tb1.getString(),operator.getString(operator.getSelectedIndex());
closeRecord();
showAlert(“成功保存登录凭据!!”;
}
}
////==============================================================================/////
///记录管理
公开作废openRecord(){
试一试{
record=RecordStore.openRecordStore(REC_STORE,true);
}捕获(例外e){
db(e.toString());
}
}
公共档案{
试一试{
closeRecordStore();
}捕获(例外e){
db(e.toString());
}
}
公共记录(){
if(RecordStore.listRecordStores()!=null){
试一试{
RecordStore.deleteRecordStore(记录存储);
}捕获(例外e){
db(e.toString());
}
}
}
public void writeRecord(字符串登录标识、字符串密码、字符串运算符名称){
字符串凭证=登录id+”,“+pwd+”,“+操作员姓名;
byte[]rec=credential.getBytes();
试一试{
if(login_id.length()>10 | | login_id.length()<10){
showAlert(“请输入有效的登录Id”);
}else if(pwd.length()<1){
showAlert(“请输入密码!!”);
}否则{
record.addRecord(rec,0,rec.length);
}
}捕获(例外e){
db(e.toString());
}
}
私有void showAlert(字符串错误){
警报a=新警报(“”);
a、 设置字符串(错误);
a、 setTimeout(警报。永久);
显示设置电流(a);
}
公共无效读取记录(){
试一试{
if(record.getNumRecords()>0){
比较器comp=新比较器();
RecordEnumeration re=记录。enumerateRecords(null、comp、false);
while(re.hasNextElement()){
String str=新字符串(re.nextRecord());
showarert(str);
}
}
}捕获(例外e){
db(e.toString());
}
}
私有void数据库(字符串错误){