Get battery level in Java

白昼怎懂夜的黑 提交于 2020-01-01 07:08:09

问题


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


回答1:


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




回答2:


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.




回答3:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!