简体   繁体   English

在特定时间执行任务

[英]Having a task performed at a certain time

So I am using the Robot class in Java to perform a task automatically during the night. 因此,我正在使用Java中的Robot类在夜间自动执行任务。 I need it to do the task at 1:10:10 am, but it isn't working with my tests. 我需要它在上午1:10:10进行任务,但是它不适用于我的测试。 I make the time match with current time, but add a minute for testing. 我使时间与当前时间匹配,但要增加一分钟进行测试。 It doesn't perform the task. 它不执行任务。 Here is my (edited, made the integer a boolean now also) main code: 这是我的(已编辑,现在也使整数成为布尔值)主要代码:

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;
        }
    }
}                    

And here are my methods: 这是我的方法:

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);
}

So, I hope anyone can enlighten me on why it isn't doing the task. 因此,我希望任何人都能启发我为什么它没有完成任务。 :) :)

There are two debug points you can follow up with - 您可以跟踪两个调试点-

  1. press should be initialized with press = 0 to meet the while condition. press应被初始化press = 0 ,满足条件时。 Though that seems to be an infinite loop post that. 尽管那似乎是一个无限循环的话题。 Unless your code reaches an exception. 除非您的代码遇到异常。

  2. Within your if condition, try changing to 在您的if条件下,尝试更改为

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

since you are comparing two strings and not an integer value here. 因为您要比较两个字符串,而不是整数。

I have figured out my own question! 我已经解决了自己的问题! It turns out that it only checked the time when I press the button, so it never checked the time after that. 事实证明,它仅在我按下按钮时检查时间,因此从不检查此后的时间。 Here is the fixed code: 这是固定代码:

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