您可能已经解决了这个问题,但对于其他问题,您可以按照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或任何合适的值。



