【问题标题】:Java Robot: Repeating every 30 minutesJava 机器人:每 30 分钟重复一次
【发布时间】:2013-12-01 11:13:04
【问题描述】:

我想做的是每 30 分钟运行一次这个机器人。它的作用是截取屏幕截图,以便我查看内容。我尝试使用

    thread.sleep();

但这对我不起作用。我将它设置为一个小的间隔,看看它是否真的可以工作,它只运行一次然后停止。我对 Java 很陌生,但我没有过多地使用 Java 机器人类或任何类型的循环。这是我的课...

public class usiwa{


public static void main(String[] args) throws AWTException, IOException, InterruptedException{


    Robot bot = new Robot();
    Date date = new Date();
    Random ra = new Random();

    int x0 = MouseInfo.getPointerInfo().getLocation().x;
    int y0 = MouseInfo.getPointerInfo().getLocation().y;

    int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
    int y1 = ra.nextInt(80 - 70 + 1) + 70;

    int dx = x1 - x0;
    int dy = y1 - y0;

            // time in msecs
    int t = 1000;

    int res = Math.max(dx, dy);

    if(res > t) res = t;
    int d = t/res;

    float inv = (float) 1/(res - 1);
    float a = 0;

    long s = 0;
    long e = 0;
    s = System.currentTimeMillis();

    for(int i = 0; i < res; i++) {
        a += inv;

        bot.mouseMove(x0 + (int) (a*dx), y0 + (int) (a*dy));
        bot.delay(d);



    }
    e = System.currentTimeMillis();
    System.out.println("Total time: " + (float) (e - s)/1000);


    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);

    bot.delay(3000);

    Rectangle r = new Rectangle(0, 0, 1000, 1000);
    BufferedImage p = bot.createScreenCapture(r);
    DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy-hh.mm.ss a");
    ImageIO.write(p, "png" , new File("C:/Users/Kalob_2/Desktop/Tracker/" + dateFormat.format(date) + ".png"));




}



}

我想做的就是让上述所有动作每 30 分钟重复一次。

感谢您的帮助。

【问题讨论】:

标签: java loops awtrobot


【解决方案1】:

这是您重复任务的方式。将现有代码包装在这个 while 循环中:

while(true) {
    // do something
    Thread.sleep(1000); // 1 second, the parameter is miliseconds
}

或者,创建一个 cron 作业来运行您的代码。以下将每 30 分钟重复一次。

30 * * * *

【讨论】:

    【解决方案2】:

    如果你想暂停你的线程,你可以这样做:

    Thread.sleep(30*60*1000); \\ 30 minutes
    

    但你最好使用ScheduledExecutorService

    【讨论】:

      【解决方案3】:
      public class usiwa {
      
           public static void main(String[] args) throws AWTException, IOException,
              InterruptedException {
      
              Robot bot = new Robot();
              Date date = new Date();
              Random ra = new Random();
      
              while (true) {
                  int x0 = MouseInfo.getPointerInfo().getLocation().x;
                  int y0 = MouseInfo.getPointerInfo().getLocation().y;
      
                  int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
                  int y1 = ra.nextInt(80 - 70 + 1) + 70;
      
                  int dx = x1 - x0;
                  int dy = y1 - y0;
      
                  // time in msecs
                  int t = 1000;
      
                  int res = Math.max(dx, dy);
      
                  if (res > t)
                  res = t;
                  int d = t / res;
      
                  float inv = (float) 1 / (res - 1);
                  float a = 0;
      
                  long s = 0;
                  long e = 0;
                  s = System.currentTimeMillis();
      
                  for (int i = 0; i < res; i++) {
                      a += inv;
      
                      bot.mouseMove(x0 + (int) (a * dx), y0 + (int) (a * dy));
                      bot.delay(d);
      
                  }
                  e = System.currentTimeMillis();
                  System.out.println("Total time: " + (float) (e - s) / 1000);
      
                  bot.mousePress(InputEvent.BUTTON1_MASK);
                  bot.mouseRelease(InputEvent.BUTTON1_MASK);
      
                  bot.delay(3000);
      
                  Rectangle r = new Rectangle(0, 0, 1000, 1000);
                  BufferedImage p = bot.createScreenCapture(r);
                  DateFormat dateFormat = new SimpleDateFormat(
                      "MM_dd_yyyy-hh.mm.ss a");
                  ImageIO.write(p, "png",
                      new File("C:/Users/Kalob_2/Desktop/Tracker/"
                              + dateFormat.format(date) + ".png"));
              }
      
          }
      
      }
      

      【讨论】:

      • Java 不是 Cwhile(1) 不会编译。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 1970-01-01
      相关资源
      最近更新 更多