游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1273|回复: 0

保护自己的MIDlet程序之TimeProtector wxh zt

[复制链接]

1367

主题

1993

帖子

2118

积分

金牌会员

Rank: 6Rank: 6

积分
2118
发表于 2005-11-1 16:11:00 | 显示全部楼层 |阅读模式


作者:黑洞 我们先来设想他所具有的功能:
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
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-22 11:49

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表