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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…