Mac - 获取电池/充电状态(插入或未插入) [英] Mac - Get Battery/Charging Status (Plugged in or not)
问题描述
我正在建立一个需要每分钟轮询服务器的Mac OSX应用程序,如果用户愿意,甚至更少。不幸的是,服务不支持push ...
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:
- 轮询电池轮询间隔轮询
- 充电时间间隔
如何获得Objective C中充电器的状态?我真的不在乎实际的百分比,只有当笔记本电脑插入或不。显然,这对于台式机无所谓,所以希望有一个适用于笔记本电脑和台式机的解决方案。
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.
推荐答案
请查看 IOPowerSources API。
首先调用 IOPSCopyPowerSourcesInfo()
,然后 IOPSCopyPowerSourcesList()
获取所有可用功率的列表来源。 IOPSGetPowerSourceDescription()
将返回包含特定电源信息的字典。根据文档,键 kIOPSPowerSourceStateKey
描述了当前电源。 kIOPSBatteryPowerValue
表示电源正在绘制内部电源; kIOPSACPowerValue
表示电源连接到外部电源。
First call IOPSCopyPowerSourcesInfo()
, then IOPSCopyPowerSourcesList()
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 key kIOPSPowerSourceStateKey
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."
您还可以设置通知电源变化与 IOPSNotificationCreateRunLoopSource()
。
You can also set up a notification when the power sources change with IOPSNotificationCreateRunLoopSource()
.
(NB:我没有测试任何这一点,只是看看文档。)
(NB: I haven't tested any of this, just looked at the documentation.)
这篇关于Mac - 获取电池/充电状态(插入或未插入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!