I need to know how to get how much power is left in a laptop battery on Windows XP. It needs to be in a percent, or something I can convert to a percent like capacity and current charge. I've done google searches and not found much of any use.
I've done work with Visual Basic in the past and I know that you can call one of the Windows APIs to get all the information about the battery and even the statistics Windows keeps track of for more accurate life time predictions. I'm thinking if there's not a way to do this entirely in Java, maybe I could call the Windows API (I've never done this in Java though) to get the info.
Anybody have any helpful information, pointers, or links?
Life is 10% what happens and 90% how you react to it.
Attitude is everything.
I'm certain that you can't do this using only Java. To call a Windows API from Java, you would probably want to use JNI. Alternately if there's a way to do this from a command line, you could use Runtime.getRuntime().exec(command) to execute the command. Then take that Process and use getInputStream() to read whatever the command prints out. Those are the main two techniques for doing non-Java stuff from Java.
If you have the professional version of XP, you can do a Runtime.exec() of the "typeperf" command. From the command line, you can use the "-qx" switch to find the battery counter. And then do a listing of that counter. There are also modes to print it in different formats, on sampling intervals, etc.
Once you find the exact command that suit your needs, then use Runtime.exec() from Java.
So let's suppose I wanted to use the 'GetSystemPowerStatus' command out of the 'Kernal32.dll' Windows API (this is the one a Visual Basic programmer would call) How would I get Java to communicate with this C++ library?
Joined: Jan 30, 2000
You'd want to use JNI - the Wikipedia article I linked to gives a good overview, and has links to additional resources. Basically you'd declare a native method in your Java class:
Note there's no implementation - you'd write that in your C/C++ code. There are a number of details to handle here, so it would be best if you read up on the link provided, and maybe check out some of the other resources. Personally I haven't done this in ages, and don't really remember all the details anyway.