MIDP的子系统Record Management System提供了MIDlet的持久性存储,精通MIDP子系统RMS系列文章对其使用进行了具体介绍。本文讲述如何使用RMS提供的功能实现应用程序的定制功能??自动登陆。
我们的设计思路非常简单,在RecordStore中存储用户的设置和用户的信息(用户名和密码),假如用户选择自动登陆的话,那么下次当用户想联网的时候将跳过登陆界面,系统会从RecordStore中读取用户和密码,经过服务器的验证后转入到适当的界面。我对整个程序进行了简化,我们不进行联网,对信息的存储也都从简,只是为了说明RMS实现应用程序定制的思路,因此给出的代码并没有全面测试和优化。下面是程序的截图
<center[[The No.1 Picture.]]</center
我们用Account和Preference分别存储用户信息和用户的个性化设置,同样在这两个类中提供序列化的方法,这样方便我们从RecordStore中读取和写入。这里只给出Preference类的代码,Account类似。
package com.j2medev.autologin;
import Java.io.*;
public class Preference
{
private boolean autoLogin;
public Preference(boolean _autoLogin)
{
this.autoLogin = _autoLogin;
}
public Preference()
{
}
public void serialize(DataOutputStream dos) throws IOException
{
dos.writeBoolean(autoLogin);
}
public static Preference deserialize(DataInputStream dis)
throws IOException
{
Preference preference = new Preference();
preference.setAutoLogin(dis.readBoolean());
return preference;
}
public boolean isAutoLogin()
{
return autoLogin;
}
public void setAutoLogin(boolean autoLogin)
{
this.autoLogin = autoLogin;
}
}
我们需要一个Model类来处理读取和写入RecordStore数据的逻辑,它也非常简单。为了简化程序我们规定首先写入Account然后写入Preference,这样我们读取的时候只要通过recordID分别为1和2来读取了,在实际使用的时候通常会比较复杂,我们要借助过滤器等查找,可以参考我的基于MIDP1.0实现个人通讯录。
package com.j2medev.autologin;
import javax.microedition.rms.*;
import java.io.*;
public class Model
{
private RecordStore accountStore;
public static final String RNAME = "accountstore";
public Model()
{
try
{
accountStore = RecordStore.openRecordStore(RNAME, true);
} catch (RecordStoreException e)
{
e.printStackTrace();
}
}
public void closeRecordStore()
{
try
{
accountStore.closeRecordStore();
} catch (RecordStoreException e)
{
e.printStackTrace();
}
}
public void saveAccount(Account account)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try
{
account.serialize(dos);
byte[] data = baos.toByteArray();
accountStore.addRecord(data, 0, data.length);
baos.close();
} catch (IOException e)
{
e.printStackTrace();
} catch (RecordStoreException e)
{
e.printStackTrace();
}
}
public Account getAccount(int recordID)
{
try
{
if (accountStore.getNumRecords() 0)
{
byte[] data = accountStore.getRecord(recordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Account account = Account.deserialize(dis);
bais.close();
return account;
}
return null;
} catch (IOException e)
{
return null;
} catch (RecordStoreException e)
{
return null;
}
}
public void savePreference(Preference preference)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try
{
preference.serialize(dos);
byte[] data = baos.toByteArray();
accountStore.addRecord(data, 0, data.length);
baos.close();
} catch (IOException e)
{
e.printStackTrace();
} catch (RecordStoreException e)
{
e.printStackTrace();
}
}
public Preference getPreference(int recordID)
{
try
{
if (accountStore.getNumRecords() 0)
{
byte[] data = accountStore.getRecord(recordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
Preference preference = Preference.deserialize(dis);
bais.close();
return preference;
}
return null;
} catch (IOException e)
{
return null;
} catch (RecordStoreException e)
{
return null;
}
}
}
MIDlet的设计同样很简单,直接给出代码。整个程序的代码可以从这里下载
package com.j2medev.autologin;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class LoginMIDlet extends MIDlet implements CommandListener
{
private Display display;
private Form loginForm;
private Form sUCcessForm;
private TextField userName;
private TextField passWord;
private ChoiceGroup autoLogin;
private Model model;
public static final Command ConnCMD = new Command("Connect", Command.OK, 1);
protected void startApp() throws MIDletStateChangeException
{
initMIDlet();
Preference p = model.getPreference(2);
if (p == null !p.isAutoLogin())
{
display.setCurrent(loginForm);
} else if (p.isAutoLogin())
{
Account account = model.getAccount(1);
System.out.println("username:" + account.getUsrName() + "password:"
+ account.getPassword());
display.setCurrent(successForm);
}
}
public void initMIDlet()
{
model = new Model();
display = Display.getDisplay(this);
loginForm = new Form("LoginForm");
userName = new TextField("username", null, 20, TextField.ANY);
password = new TextField("password", null, 20, TextField.PASSWORD);
autoLogin = new ChoiceGroup("AutoLogin", Choice.MULTIPLE,
new String[] { "remember me" }, null);
loginForm.append(userName);
loginForm.append(password);
loginForm.append(autoLogin);
loginForm.addCommand(ConnCMD);
loginForm.