MIDP的子系統Record Management System提供了MIDlet的持久性存儲,精通MIDP子系統RMS系列文章對其使用進行了詳細介紹。本文講述如何使用RMS提供的功能實現應用程序的定制功能??自動登陸。
我們的設計思路非常簡單,在RecordStore中存儲用戶的設置和用戶的信息(用戶名和密碼),如果用戶選擇自動登陸的話,那麼下次當用戶想聯網的時候將跳過登陸界面,系統會從RecordStore中讀取用戶和密碼,經過服務器的驗證後轉入到適當的界面。我對整個程序進行了簡化,我們不進行聯網,對信息的存儲也都從簡,只是為了說明RMS實現應用程序定制的思路,因此給出的代碼並沒有全面測試和優化。下面是程序的截圖
我們用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.