I would like to make a java app that shows the current battery level of my mac OS X.
I have read get OS-level system information and was wondering how I could modify this to make it display the current % of battery
Thanks
I think you have two options.
One is to use JNI to invoke native code to get the battery level.
The other is to invoke the application pmset
using System.exec
in java and parse the output. I think the arguments to retrieve the battery level is pmset -g ps
but you better check the man page
You'll need to use the Mac OS X APIs:
You can get information about power sources and UPS (uninterruptible power supply) devices using the I/O Kit’s power-source API located in /System/Library/Frameworks/IOKit.framework/Headers/ps. The header files in this folder, IOPowerSources.h and IOPSKeys.h, contain methods and keys to extract information about both external and internal power sources. For example, an application can get a list of attached power sources, request notifications for changes in its power sources, and determine how much power is left in a battery.
[edit]
You will need to use JNI to access the IOKit framework.
Without some OS X equivalent to /proc on linux, I doubt you'll get this done without a System.exec or a JNI call.
来源:https://stackoverflow.com/questions/1632365/get-battery-level-in-java