接收笔记本电脑OSX Cocoa中电源线打开/关闭的通知
全部, 在Mac笔记本电脑中插入/拔出电源线时,OSX是否会发送任何通知?或者从扩展底座上拔下笔记本电脑 它确实会在关机/重启/唤醒等情况下发送一些信息,但我没有找到关于该特定事件的任何信息 我错过什么了吗?或者这是不可用的接收笔记本电脑OSX Cocoa中电源线打开/关闭的通知,cocoa,notifications,Cocoa,Notifications,全部, 在Mac笔记本电脑中插入/拔出电源线时,OSX是否会发送任何通知?或者从扩展底座上拔下笔记本电脑 它确实会在关机/重启/唤醒等情况下发送一些信息,但我没有找到关于该特定事件的任何信息 我错过什么了吗?或者这是不可用的 蒂亚 没有高级通知,但只要电源状态发生变化,IOPower资源框架就可以向您的运行循环发布消息 我的项目有多个PowerCondition对象,它们监视当前电源的各个方面(A/C与电池、剩余电池量等) 下面是PowerCondition类的一个片段,展示了如何通过运行循环观
蒂亚 没有高级通知,但只要电源状态发生变化,IOPower资源框架就可以向您的运行循环发布消息 我的项目有多个
PowerCondition
对象,它们监视当前电源的各个方面(A/C与电池、剩余电池量等)
下面是PowerCondition
类的一个片段,展示了如何通过运行循环观察电源更改事件,并将其转化为多个对象可以观察到的通知(这是相当古老的Obj-C):
请注意,回调安装在主运行循环上,因此通知将始终发布在主线程上
#define kPowerConditionChangedNotification @"PowerConditionChanged"
static NSUInteger PowerChangeListenerCount = 0;
static CFRunLoopSourceRef PowerChangeSource = NULL;
static void PowerChangeCallback(void *context)
{
// Called by CFRunLoopSourceRef when something changes
// Post a notification so that all PowerCondition observers can reevaluate
[[NSNotificationCenter defaultCenter] postNotificationName:kPowerConditionChangedNotification object:nil];
}
@interface PowerCondition
{
BOOL listening;
//...
}
- (void)powerStateDidChangeNotification:(NSNotification*)notification;
@end
// ...
@implementation PowerCondition
- (void)startMonitoringCondition
{
if (!listening)
{
// Observe the power condition change notification
DebugLogger(@"condition '%@'",[self description]);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(powerStateDidChangeNotification:)
name:kPowerConditionChangedNotification
object:nil];
if (PowerChangeListenerCount++==0)
{
// This is the first observer: create and install the run loop source that will fire the notification
BetaAssert(PowerChangeSource==NULL,@"zero PowerChangeListenerCount, but PowerChangeSource!=NULL");
PowerChangeSource = IOPSNotificationCreateRunLoopSource(PowerChangeCallback,NULL);
CFRunLoopAddSource([[NSRunLoop mainRunLoop] getCFRunLoop],PowerChangeSource,kCFRunLoopCommonModes);
DebugLoggerMessage(@"installed PowerChangeSource(PowerChangeCallback) in run loop");
}
listening = YES;
previousTestState = -1; // neither YES nor NO
}
}
- (void)stopMonitoringCondition
{
if (listening)
{
// Stop observing power condition change notifications
PowerLogger(@"condition '%@'",[self description]);
[[NSNotificationCenter defaultCenter] removeObserver:self];
BetaAssert(PowerChangeListenerCount!=0,@"PowerChangeListenerCount==0");
if (--PowerChangeListenerCount==0)
{
// This was the last power change observer: remove the run loop source the fires the notifications
BetaAssertNotNil(PowerChangeSource);
CFRunLoopRemoveSource([[NSRunLoop mainRunLoop] getCFRunLoop],PowerChangeSource,kCFRunLoopCommonModes);
CFRelease(PowerChangeSource);
PowerChangeSource = NULL;
DebugLoggerMessage(@"removed PowerChangeSource(PowerChangeCallback) from run loop");
}
listening = NO;
}
}
- (void)powerStateDidChangeNotification:(NSNotification*)notification
{
// Evaluate power state here
BOOL battery = NO; // assume unlimited/external power
double capacityRemaining = 1.0; // assume 100%
// First, determine if the system's active power source is AC or battery
CFTypeRef powerBlob = IOPSCopyPowerSourcesInfo();
CFArrayRef powerSourcesRef = IOPSCopyPowerSourcesList(powerBlob);
CFIndex count = CFArrayGetCount(powerSourcesRef);
if (count!=0)
{
// There's precious little explination what the meaning of the different power sources
// are or why there would be more than one. As far as I can tell, everything can be
// determined by obtaining the first (and probably only) power source description.
NSDictionary* powerInfo = (__bridge id)IOPSGetPowerSourceDescription(powerBlob,CFArrayGetValueAtIndex(powerSourcesRef,0));
if (![powerInfo[@kIOPSPowerSourceStateKey] isEqualToString:@kIOPSACPowerValue])
{
// Power source is not AC (must be battery or UPS)
battery = YES;
// Calculate the remaining capacity, as a fraction
NSNumber* capacityValue = powerInfo[@kIOPSCurrentCapacityKey];
NSNumber* maxValue = powerInfo[@kIOPSMaxCapacityKey];
if (capacityValue!=nil && maxValue!=nil)
capacityRemaining = [capacityValue doubleValue]/[maxValue doubleValue];
DebugLogger(@"power source is '%@', battery=%d, capacity=%@, max=%@, remaining=%f%%",
powerInfo[@kIOPSPowerSourceStateKey],
(int)battery,
capacityValue,maxValue,capacityRemaining*100);
}
}
// ...
}