Mac - 获取电池/充电状态(是否已插入)
我正在构建一个 Mac OSX 应用程序,需要每分钟轮询一次服务器,如果用户愿意,甚至可以更少。不幸的是,该服务不支持推送...
无论如何,我想向用户提供两个选项:
- 电池轮询间隔
- 充电时轮询间隔
如何在 Objective C 中获取充电器的状态?我并不关心实际百分比,只关心笔记本电脑是否插入。显然,这对于台式机来说并不重要,因此希望有一个适用于笔记本电脑和台式机的解决方案。
I'm building a Mac OSX app that needs to poll a server every minute, or even less if the user wishes. Unfortunately the service doesn't support push...
Anyways, I would like to provide two options to the user:
- Polling interval on battery Polling
- interval while charging
How would I get the state of the charger in Objective C? I don't really care about the actual percentage, only if the laptop is plugged in or not. Obviously, this doesn't matter for desktops, so hopefully there is a solution that works for laptops and desktops.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 IOPowerSources API。
首先调用
IOPSCopyPowerSourcesInfo()
,然后调用IOPSCopyPowerSourcesList()
以获取所有可用电源的列表。IOPSGetPowerSourceDescription()
将返回包含有关特定电源信息的字典。根据文档,键kIOPSPowerSourceStateKey
描述了“当前的电源。kIOPSBatteryPowerValue
表示电源正在消耗内部电源;kIOPSACPowerValue
表示电源已连接至外部电源。”您还可以使用
IOPSNotificationCreateRunLoopSource()
设置电源更改时的通知。(注意:我还没有测试过这些,只是查看了文档。)
Have a look at the IOPowerSources API.
First call
IOPSCopyPowerSourcesInfo()
, thenIOPSCopyPowerSourcesList()
to get a list of all available power sources.IOPSGetPowerSourceDescription()
will return a dictionary with information about a particular power source. According to the documentation, the keykIOPSPowerSourceStateKey
describes "the current source of power.kIOPSBatteryPowerValue
indicates power source is drawing internal power;kIOPSACPowerValue
indicates power source is connected to an external power source."You can also set up a notification when the power sources change with
IOPSNotificationCreateRunLoopSource()
.(NB: I haven't tested any of this, just looked at the documentation.)
尽管这个问题确实已经有了一个公认的答案,这使我找到了解决方案,但点击很多很多损坏的链接是很痛苦的。
这是我的解决方案:
IOKit.framework
#import
代码:
Although this question does already have an accepted answer which led me to my solution, it was painful to click through lots and lots of broken links.
Here is my solution:
IOKit.framework
#import <IOKit/ps/IOPowerSources.h>
Code: