簡體   English   中英

如何使用 java 在 Linux 和 Mac OS 中檢測工作站/系統屏幕鎖定/解鎖?

[英]How to Detect workstation/System Screen Lock/unlock in Linux and Mac OS using java?

當 mac/linux 機器進入睡眠和喚醒時,我想要一個事件。 任何人都可以建議使用 java 的解決方案,它可以檢測機器鎖定和解鎖 state。

我嘗試使用 java 運行一些命令行,它給出了 output 顯示屏幕保護程序狀態,但該過程不可靠,因為它因操作系統版本而異。

我為 linux 發射的命令是

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

對於 mac 是

echo $((`ioreg -n IODisplayWrangler | grep -i IOPowerManagement | perl -pe 's/^.*DevicePowerState\\\"=([0-9]+).*$/\\1/'`))

理想情況下,您需要Desktop.addAppEventListener 使用它,不需要本地命令:

if (Desktop.isDesktopSupported()) {
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.APP_EVENT_SYSTEM_SLEEP)) {
        desktop.addAppEventListener(new SystemSleepListener() {
            @Override
            public void systemAboutToSleep(SystemSleepEvent event) {
                System.out.println("System is going to sleep.");
            }

            @Override
            public void systemAwoke(SystemSleepEvent event) {
                System.out.println("System is exiting sleep mode.");
            }
        });
    }
}

但是,如果我們查看 JDK 源代碼中的gtk3_interface.c ,並在該文件中搜索ADD_SUPPORTED_ACTION ,就會發現 Linux 和 Unix 中唯一支持的桌面操作是打開、瀏覽和郵件。

在那種情況下,外部命令似乎是唯一的選擇。 (我更喜歡upower --monitor而不是 gnome-screensaver-command。)

您不應使用 grep 或 perl。您不需要它們。 你有 Java。Java 有一個功能齊全的正則表達式 package ,它可以做 grep 可以做的所有事情,perl 可以做的大部分事情:

String os = System.getProperty("os.name");
if (os.contains("Linux")) {
    ProcessBuilder builder = new ProcessBuilder("upower", "--monitor");
    builder.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process upower = builder.start();

    CompletableFuture.runAsync(() -> {
        try (BufferedReader output = upower.inputReader()) {
            String line;
            while ((line = output.readLine()) != null) {
                if (line.contains("sleep") || line.contains("Sleep")) {
                    System.out.println("System is going to sleep.");
                }
                if (line.contains("hibernate") || line.contains("Hibernate")) {
                    System.out.println("System is hibernating.");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
} else if (os.contains("Mac")) {
    ProcessBuilder builder =
        new ProcessBuilder("ioreg", "-n", "IODisplayWrangler");
    builder.redirectError(ProcessBuilder.Redirect.INHERIT);
    Process ioreg = builder.start();

    CompletableFuture.runAsync(() -> {
        try (BufferedReader output = ioreg.inputReader()) {
            Matcher powerStateMatcher =
                Pattern.compile("DevicePowerState\"=([0-9]+)".matcher("");

            String line;
            while ((line = output.readLine()) != null) {
                if (line.contains("IOPowerManagement") &&
                    powerStateMatcher.reset(line).find()) {

                    int newState = Integer.parseInt(
                        powerStateMatcher.group(1));
                    System.out.println("New device state is " + newState);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM