可可获取电源适配器状态 [英] Cocoa get Power Adapter Status

查看:103
本文介绍了可可获取电源适配器状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理备份应用程序,并且最近收到了很多关于要添加功能的请求,其中最重要的一项就是添加了一个复选框,例如 Time Machine时使用电池备份

I'm currently working on a Backup Application, and I have received a lot of requests for features to be added recently, and the top one of them is adding a checkbox like "Back Up when on Battery Power" like Time Machine has.

那么,有没有一种方法可以获取电源适配器的状态(已插入并已插入)?我假设如果有一台台式机,例如iMac等,我可能一直都会插上电源。因此,我需要检测计算机是否为便携式计算机的方法。

So, is there a way I can get the status of the Power Adapter (plugged in and plugged in)? I assume that if one had a Desktop Mac, like iMac, etc, I would probably just get Plugged In all the time. Therefore, I need means of detecting if the computer is a portable or not.

我认为IOKit是一个可以查看的库,但是我根本无法在其中找到任何东西。在该框架上几乎不存在的文档。然后,由于它是一个较旧的Carbon框架,是否有办法注册在电源状态更改时调用的回调?这样,我就可以正确实现此复选框的功能。

I assume IOKit would be a library to look at, but I simply could not find anything in the docs, that are barely existent anyways on that Framework. Then, since it is an older Carbon Framework, is there a way to register a callback that gets called when that power state changes? That way I can properly implement this checkbox's functionality.

在帮助您澄清此主题方面,我将不胜感激。

I'd appreciate help in clarifying this subject.

推荐答案

IOPowerSources.h具有您需要的功能。从 IOPSNotificationCreateRunLoopSource()开始创建运行循环源,然后在回调中以 IOPSCopyPowerSourcesInfo()并逐步解决。可能有更好的示例,但是此文件似乎是对其完成方式的合理证明;足以让您入门。

IOPowerSources.h has the functions you need. Start with IOPSNotificationCreateRunLoopSource() to create a run loop source and in your callback interrogate the power source info starting with IOPSCopyPowerSourcesInfo() and working your way down. There may be better examples, but this file appears to be a reasonable demonstration of how it's done; enough to get you started.

这篇关于可可获取电源适配器状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆