从java监控笔记本电脑的电池或电源
我正在开发一个应用程序来监视笔记本电脑电源的存在。 如果停电或恢复供电,它会通过电子邮件通知我。 它还将通过电子邮件进行应用程序监视和控制(基本上是通过电子邮件从我的办公室控制我的笔记本电脑)。 我已经完成了电子邮件接口,但我不知道如何从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Linux 上,您可以使用 /proc/acpi/battery/
On linux, you can use /proc/acpi/battery/
在 google 上快速搜索一下,就会发现 sourceforge 上的 java acpi 库。 不过自 2004 年以来就没有更新过。
A quick google search turns up a java acpi library on sourceforge. Hasn't been updated since 2004 though.
以下是使用
在 Windows 上运行的代码SYSTEM_POWER_STATUS
结构。请注意,您需要将
jna
添加到您的 (Maven) 依赖项中上班。在您的代码中这样调用它:
结果:
这已派生自 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.In your code call it like this:
Result:
This has been derived off BalsusC's answer.
处理此问题的一种快速而肮脏的方法是调用本机程序(通过 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.
您可能已经解决了这个问题,但对于其他问题 - 您可以按照 Adam Crume 建议的方式进行操作,使用已经编写的脚本 battstat .bat 适用于 Windows XP 及更高版本。
以下是结果函数的示例:
或者您可以简化脚本以直接返回 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:
Or you can simplify the script to return directly True/False or whatever fits.