分享
 
 
 

应用RMS实现用户自动登陆功能

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

作者:mingjava 文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=117

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.setCommandListener(this);

successForm = new Form("OK");

successForm.append("Ok you have connected to server");

}

protected void pauseApp()

{

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException

{

}

public void commandAction(Command arg0, Displayable arg1)

{

String _userName;

String _password;

boolean auto = false;

if (arg0 == ConnCMD)

{

_userName = userName.getString();

_password = password.getString();

auto = autoLogin.isSelected(0);

System.out.println(_userName + _password + auto);

if (auto)

{

Account account = new Account(_userName, _password);

model.saveAccount(account);

Preference preference = new Preference(auto);

model.savePreference(preference);

}

display.setCurrent(successForm);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有