控制上网!!!(版本之1.0)

来源:互联网 发布:ziprar解压软件下载 编辑:程序博客网 时间:2024/06/04 19:38

控制上网! 控制上网!(放到开机启动项中)


package com.sxz.timecontroal;import java.util.Calendar;/** * @author sunxuezhi 2012/01/28 */public class TimeControal implements Runnable {        private static int countFiveMinute = 0;        /**     * 程序每次检查的间隔时间设置为5分钟(300000)。     */    private static final int TIME_BETWEEN_CHECK = 300000;        /**     * 程序最多检查的次数( 次数(36) * 时间间隔 = 限定开机时间 )。     */    private static final int MAX_CHECK_COUNT = 36;        /**     * 线程     */    public void run() {                try {            // 控制1:超过当前时间22:30            if(TimeControal.nowTime()){                // 关机                TimeControal.shutdown();            }                        // 每隔5分钟再检查一次            Thread.sleep(TIME_BETWEEN_CHECK);                        // 控制2:开机后时间超过3个小时            if (TimeControal.startedTime()){                // 关机                TimeControal.shutdown();            }                } catch (InterruptedException e) {                        e.printStackTrace();        }            }        /**     *       * 当前时间判断,超过10:30关机     *      * 超过限定时间,返回ture     *      */    public static boolean nowTime(){                // 是否超过限定时间,判定flag        boolean flag = false;                Calendar date = Calendar.getInstance();        // 设置当前时间。        date.setTimeInMillis(System.currentTimeMillis());                if(date.get(Calendar.HOUR_OF_DAY) >= 23 || date.get(Calendar.HOUR_OF_DAY) < 6){            flag = true;        }                if(date.get(Calendar.HOUR_OF_DAY) == 22 && date.get(Calendar.MINUTE) > 30){            flag = true;        }                return flag;    }        /**     *       * 开机后时间是否超过3个小时判定。     *      * 如果开机后达到3个小时,返回ture     *      */    public static boolean startedTime(){                // 是否达到3个小时,判定flag        boolean flag = false;                    countFiveMinute ++;                // 有36次5分钟的计时,表明已经3个小时了。        if(countFiveMinute >= MAX_CHECK_COUNT){            flag = true;        }                return flag;    }            /**     * 1分钟内关机     */    public static void shutdown(){        try {             // 60 秒内自动关机            Process process = Runtime.getRuntime().                exec("shutdown -s -t 60");                      } catch (Exception e) {             e.printStackTrace();           }    }}

主函数

package com.sxz.timecontroal;/** * @author sunxuezhi 2012/01/28 */public class MainTimeControal {/** * 时间控制 */public static void main(String[] args) {TimeControal timeContraol = new TimeControal();Thread thread = new Thread(timeContraol);while (true) {// 使程序一直处于运行状态thread.run();}}}

注意:使用Eclipse导出jar文件时,不要忘了选主函数~






原创粉丝点击