第一部分介绍了protector,并实现了一个最简单的protector:onceprotector
第二部分将实现一个timeprotector
我们先来设想他所具有的功能:
1。没有到指定时间,他是处于激活状态的(check函数返回true);
2。过了指定时间,他就不能激活;
3。一旦过期,即使用户调整了时间也不再激活。
嗯,明确了目的,下面就来继续。
涉及到了时间问题,就来说说如何处理时间。midp有date类型,但是提供的功能非常有限。
构造函数无法接受人们熟悉的字符串来初始化。
我们必须使用calendar来做这些工作,当然,如果long在你眼里看来就是time(黑客帝国?)。。。
我是这样处理的,看timeprotector的构造函数:
public timeprotector(midlet app,int year,int month,int date)
{
super(app);
calendar c=calendar.getinstance();
c.set(calendar.year,year);
c.set(calendar.month,month-1);
c.set(calendar.date,date);
date_=c.gettime();
}
这里要注意一个问题,就是month-1的问题,set month 9就是10月的意思。
timeprotector有一个字段date_,可以用date_.gettime()直接和system.currenttimemillis()比较大小。
那么我们如何实现第三个设想呢?如何做到即使用户修改时间也无法继续使用呢?
不妨再用用rms来做个标记,表明已经过期:)
下面就是那个决定命运的check函数:
public boolean check()
{
boolean r=true;
try
{
recordstore rs=recordstore.openrecordstore(namerms,true);
recordenumeration e=rs.enumeraterecords(null,null,false);
if(e.hasnextelement())
{
r=false;
}
else
{
if(date_.gettime()<system.currenttimemillis())
{
writetag(rs);
r=false;
}
else
{
r=true;
}
}
rs.closerecordstore();
}
catch(exception ex)
{
r=false;
}
if(!r)
showui();
return r;
}
好了,这就是我的timeprotector的实现,你是怎么实现的呢?
timeprotector的使用:
在startapp的开头加入if(!new timeprotector(this,2005,10,19).check())return;
就是这么简单。准备好,下一篇就来实现snprotector:)
有意见或建议请联系vmlinuxx@gmail.com,共同学习,共同提高:)
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器