Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

windows - How to determine if a screensaver is running in Java?

My Application periodicly shows information on the screen. But if the screenshot is active, the application should wait until the user returns.

Is there any way to determine if the screensaver is running?

I don't need a clean solution, u just needs to work on windows xp.

Similar question, but other technology: How to determine that a screensaver is running?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, this is absolutely not clean, but it works as a dirty workaround:

I checks if 'any' screensaver (which have .SCR suffix) is running.

  private static boolean isScreensaverRunning() {
    List<String> p = WindowsUtils.listRunningProcesses();
    for (String s : p) {
      if (s.endsWith(".SCR")) {
    return true;
      }
    }
    return false;
  }

  public static List<String> listRunningProcesses() {
    List<String> processes = new ArrayList<String>();
    try {
      String line;
      Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
      BufferedReader input = new BufferedReader
      (new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
      if (!line.trim().equals("")) {
          // keep only the process name
          line = line.substring(1);
      processes.add(line.substring(0, line.indexOf(""")));
      }

      }
      input.close();
    }
    catch (Exception err) {
      err.printStackTrace();
    }
    return processes;
  }

Source of listRunningProcesses: http://www.rgagnon.com/javadetails/java-0593.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...