从java监控笔记本电脑的电池或电源

发布于 2024-07-27 15:53:27 字数 203 浏览 3 评论 0原文

我正在开发一个应用程序来监视笔记本电脑电源的存在。 如果停电或恢复供电,它会通过电子邮件通知我。 它还将通过电子邮件进行应用程序监视和控制(基本上是通过电子邮件从我的办公室控制我的笔记本电脑)。 我已经完成了电子邮件接口,但我不知道如何从 java 监控电源/电池供应。

如果有人能就此提供一些指导,那将会有很大的帮助。

提前致谢 ....

I am developing an application which monitors the presence of the power supply of the laptop. If there is a power cut or restoration it will intimate me over email. It will also application monitoring and controlling over email (Basically to control my laptop from my office over email). I am done with email interfacing but I have no idea on how to monitor the power supply / battery supply from java.

If any can give some pointer on this it will be of great help.

Thanks in advance ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙女 2024-08-03 15:53:28

在 Linux 上,您可以使用 /proc/acpi/battery/

On linux, you can use /proc/acpi/battery/

梦里°也失望 2024-08-03 15:53:28

在 google 上快速搜索一下,就会发现 sourceforge 上的 java acpi 库。 不过自 2004 年以来就没有更新过。

A quick google search turns up a java acpi library on sourceforge. Hasn't been updated since 2004 though.

谈场末日恋爱 2024-08-03 15:53:28

以下是使用 在 Windows 上运行的代码SYSTEM_POWER_STATUS 结构。

请注意,您需要将 jna 添加到您的 (Maven) 依赖项中上班。

import java.util.ArrayList;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public interface Kernel32 extends StdCallLibrary
{
    public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32",
            Kernel32.class);

    public class SYSTEM_POWER_STATUS extends Structure
    {
        public byte ACLineStatus;

        @Override
        protected List<String> getFieldOrder()
        {
            ArrayList<String> fields = new ArrayList<String>();
            fields.add("ACLineStatus");

            return fields;
        }

        public boolean isPlugged()
        {
            return ACLineStatus == 1;
        }
    }

    public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
}

在您的代码中这样调用它:

Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS();
Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus);

System.out.println(batteryStatus.isPlugged());

结果:

true if charger is plugged in false otherwise

这已派生自 BalsusC 的回答

Here's code that works on Windows by using the SYSTEM_POWER_STATUS structure.

Note that you need to add jna to your (Maven) dependencies for this to work.

import java.util.ArrayList;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.win32.StdCallLibrary;

public interface Kernel32 extends StdCallLibrary
{
    public Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("Kernel32",
            Kernel32.class);

    public class SYSTEM_POWER_STATUS extends Structure
    {
        public byte ACLineStatus;

        @Override
        protected List<String> getFieldOrder()
        {
            ArrayList<String> fields = new ArrayList<String>();
            fields.add("ACLineStatus");

            return fields;
        }

        public boolean isPlugged()
        {
            return ACLineStatus == 1;
        }
    }

    public int GetSystemPowerStatus(SYSTEM_POWER_STATUS result);
}

In your code call it like this:

Kernel32.SYSTEM_POWER_STATUS batteryStatus = new Kernel32.SYSTEM_POWER_STATUS();
Kernel32.INSTANCE.GetSystemPowerStatus(batteryStatus);

System.out.println(batteryStatus.isPlugged());

Result:

true if charger is plugged in false otherwise

This has been derived off BalsusC's answer.

苏璃陌 2024-08-03 15:53:28

处理此问题的一种快速而肮脏的方法是调用本机程序(通过 Runtime.exec(...))并解析输出。 在 Windows 上,本机程序可能是使用 WMI 的 VBScript。

A quick and dirty way to handle this is call a native program (via Runtime.exec(...)) and parse the output. On Windows, the native program might be VBScript that uses WMI.

眼眸里的那抹悲凉 2024-08-03 15:53:27

您可能已经解决了这个问题,但对于其他问题 - 您可以按照 Adam Crume 建议的方式进行操作,使用已经编写的脚本 battstat .bat 适用于 Windows XP 及更高版本。
以下是结果函数的示例:

private Boolean runsOnBattery() {
    try {
        Process proc = Runtime.getRuntime().exec("cmd.exe /c battstat.bat");

        BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(proc.getInputStream()));

        String s;
        while ((s = stdInput.readLine()) != null) {
            if (s.contains("mains power")) {
                return false;
            } else if (s.contains("Discharging")) {
                return true;
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return false;
}

或者您可以简化脚本以直接返回 True/False 或任何合适的值。

You have probably already solved this problem but for the others - you can do it the way Adam Crume suggests, using an already written script battstat.bat for Windows XP and higher.
Here is an example of the resulting function:

private Boolean runsOnBattery() {
    try {
        Process proc = Runtime.getRuntime().exec("cmd.exe /c battstat.bat");

        BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(proc.getInputStream()));

        String s;
        while ((s = stdInput.readLine()) != null) {
            if (s.contains("mains power")) {
                return false;
            } else if (s.contains("Discharging")) {
                return true;
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return false;
}

Or you can simplify the script to return directly True/False or whatever fits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文