WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程应用RMS实现用户自动登陆功能

应用RMS实现用户自动登陆功能
作者:不详  来源:转载  发布时间:2005-9-7 13:13:59
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(t

[1] [2]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码