Get battery level in Java

孤街醉人 提交于 2019-12-03 20:11:17

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.

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