Mac - 获取电池/充电状态(是否已插入)

发布于 2024-11-02 23:00:08 字数 242 浏览 1 评论 0原文

我正在构建一个 Mac OSX 应用程序,需要每分钟轮询一次服务器,如果用户愿意,甚至可以更少。不幸的是,该服务不支持推送...

无论如何,我想向用户提供两个选项:

  1. 电池轮询间隔
  2. 充电时轮询间隔

如何在 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:

  1. Polling interval on battery Polling
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2024-11-09 23:00:08

查看 IOPowerSources API。

首先调用 IOPSCopyPowerSourcesInfo(),然后调用 IOPSCopyPowerSourcesList() 以获取所有可用电源的列表。 IOPSGetPowerSourceDescription() 将返回包含有关特定电源信息的字典。根据文档,键kIOPSPowerSourceStateKey描述了“当前的电源。kIOPSBatteryPowerValue表示电源正在消耗内部电源;kIOPSACPowerValue表示电源已连接至外部电源。”

您还可以使用 IOPSNotificationCreateRunLoopSource() 设置电源更改时的通知。

(注意:我还没有测试过这些,只是查看了文档。)

Have a look at the IOPowerSources API.

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."

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.)

メ斷腸人バ 2024-11-09 23:00:08

尽管这个问题确实已经有了一个公认的答案,这使我找到了解决方案,但点击很多很多损坏的链接是很痛苦的。

这是我的解决方案:

  1. 添加 IOKit.framework
  2. Import #import
  3. 代码:

    CFTimeInterval timeRemaining = IOPSGetTimeRemainingEstimate();
    
    if (剩余时间 == kIOPSTimeRemainingUnlimited) {
            // 连接到插座
    } else if (timeRemaining == kIOPSTimeRemainingUnknown){
            // 剩余时间未知(最近未插电)
    } else if ((剩余时间 / 60) < 30){
            // 剩余时间不到 30 分钟
    }
    代码

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:

  1. Add IOKit.framework
  2. Import #import <IOKit/ps/IOPowerSources.h>
  3. Code:

    CFTimeInterval timeRemaining = IOPSGetTimeRemainingEstimate();
    
    if (timeRemaining == kIOPSTimeRemainingUnlimited) {
            // connected to outlet
    } else if (timeRemaining == kIOPSTimeRemainingUnknown){
            // time remaining unknown (recently unplugged)
    } else if ((timeRemaining / 60) < 30){
            // less than 30 minutes remaining
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文