从java监控笔记本电脑的电池或电源 [英] Monitoring battery or power supply of laptop from java

查看:813
本文介绍了从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.

提前致谢....

推荐答案

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

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;
}

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

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

这篇关于从java监控笔记本电脑的电池或电源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆