繁体   English   中英

在特定时间执行任务

[英]Having a task performed at a certain time

因此,我正在使用Java中的Robot类在夜间自动执行任务。 我需要它在上午1:10:10进行任务,但是它不适用于我的测试。 我使时间与当前时间匹配,但要增加一分钟进行测试。 它不执行任务。 这是我的(已编辑,现在也使整数成为布尔值)主要代码:

private void startStopButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
    String time = sdf.format(cal.getTime());
    System.out.println(time);
    press = true;
    while(press == true){
        if(time.equals("09:39:10")){
            System.out.println("well its time");
            try {
                rightClick();
                TimeUnit.SECONDS.sleep(2);
                click(573, 255);
                TimeUnit.SECONDS.sleep(2);
                click(648, 294);
                TimeUnit.SECONDS.sleep(2);
                keyPress();
                TimeUnit.SECONDS.sleep(2);
                press = false;
            } catch (AWTException | InterruptedException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
        }
    }
}                    

这是我的方法:

private void rightClick() throws AWTException{
    Robot bot = new Robot();
    bot.keyPress(KeyEvent.VK_5);
    bot.delay(500);
    bot.keyRelease(KeyEvent.VK_5);
    bot.delay(1000);
    bot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
    bot.delay(500);
    bot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
}

private void click(int x, int y) throws AWTException{
    Robot rob = new Robot();
    rob.mouseMove(x, y);
    rob.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    rob.delay(500);
    rob.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}

private void keyPress() throws AWTException{
    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_T);
    r.delay(500);
    r.keyRelease(KeyEvent.VK_T);
    r.delay(500);
}

因此,我希望任何人都能启发我为什么它没有完成任务。 :)

您可以跟踪两个调试点-

  1. press应被初始化press = 0 ,满足条件时。 尽管那似乎是一个无限循环的话题。 除非您的代码遇到异常。

  2. 在您的if条件下,尝试更改为

     if(sdf.format(cal.getTime()).equals("08:15:10") // notice the starting 0 

因为您要比较两个字符串,而不是整数。

我已经解决了自己的问题! 事实证明,它仅在我按下按钮时检查时间,因此从不检查此后的时间。 这是固定代码:

private void startStopButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    press = true;
    System.out.println(press);
    while(press == true){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        String time = sdf.format(cal.getTime());
        System.out.println(time);
        if(time.equals("10:27:10")){
            System.out.println("well its time");
            try {
                rightClick();
                TimeUnit.SECONDS.sleep(2);
                click(573, 255);
                TimeUnit.SECONDS.sleep(2);
                click(648, 294);
                TimeUnit.SECONDS.sleep(2);
                keyPress();
                TimeUnit.SECONDS.sleep(2);
                press = false;
            } catch (AWTException | InterruptedException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            break;
        }
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM