如何使用java检测ubuntu中的工作站/系统屏幕锁定?

时间:2012-01-18 06:47:27

标签: java ubuntu

我试图记下在ubuntu OS中工作的每个员工的工作站/系统屏幕锁定。我需要将这些记录存储在DataBase中。使用JAVA。我已经搜遍了所有UbUNTU的想法;但是想知道如何在Windows操作系统中做同样的事情。

2 个答案:

答案 0 :(得分:3)

来自here

gnome-screensaver-command -q | grep "is active"

使用Runtime类执行该命令并回读结果。

编辑:使用grep -q

以下是如何使用它的示例:

public class ScreenSaver {

  /*
   * Pipes are a shell feature, so you have to open a shell first.
   * 
   * You could use process.getInputStream() to read the output and parse it.
   * 
   * For productive use i would prefer using the Inputstream.
   */

  private static final String COMMAND = "gnome-screensaver-command -q |  grep -q 'is active'";

  private static final String[] OPEN_SHELL = { "/bin/sh", "-c", COMMAND };

  private static final int EXPECTED_EXIT_CODE = 0;


  public static boolean isScreenSaverActive() {
    final Runtime runtime = Runtime.getRuntime();
    Process process = null;
    try {
      /*
       * open a shell and execute the command in that shell
       */
      process = runtime.exec(OPEN_SHELL);
      /*
       * wait for the command to finish
       */
      return process.waitFor() == EXPECTED_EXIT_CODE;
    } catch(final IOException e) {
      e.printStackTrace();
    } catch(final InterruptedException e) {
      e.printStackTrace();
    }
    return false;
  }


  public static void main(final String[] args) {
    System.out.println("Screensaver is active: " + isScreenSaverActive());
  }

}

编辑:添加了监听dbus信号的perl脚本。资源: Gnome Screensaver FAQ

#!/usr/bin/perl

my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='ActiveChanged'\"";

open (IN, "$cmd |");

while (<IN>) {
    if (m/^\s+boolean true/) {
        print "*** Screensaver is active ***\n";
    } elsif (m/^\s+boolean false/) {
        print "*** Screensaver is no longer active ***\n";
    }
}

答案 1 :(得分:1)

试试这里,(类似重复),Detect workstation/System Screen Lock using Python(ubuntu)

GNOME Screensaver FAQ这应该是一个很好的参考,让你加快速度。我想你正在使用GNOME。