简体   繁体   中英

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. I need it to do the task at 1:10:10 am, but it isn't working with my tests. 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. 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(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;
        }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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