知道系统何时在Menu Extra中进入睡眠状态?

Rio*_*Rio 5 objective-c osx-snow-leopard

我为Mac开发了一个Menu Extra(menulet),但我想知道使用Menu Extra的机器是否已经进入睡眠状态.我实现了一个贪睡功能,但因为它是一个基于时间的方法,所以它相当不可靠.任何提示或建议将不胜感激!

谢谢!

zpa*_*ack 8

您可能想要NSWorkspaceWillSleepNotificationNSWorkspace查看.
诀窍是,您需要在NSWorkspace的notificationCenter上注册通知,而不是默认通知. 参见技术问答QA1340.

示例代码:

- (void) receiveSleepNote: (NSNotification*) note
{
    NSLog(@"receiveSleepNote: %@", [note name]);
}

- (void) fileNotifications
{
    // These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    // with the default notification center.
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self 
            selector:@selector(receiveSleepNote:) 
            name: NSWorkspaceWillSleepNotification object:nil];
}
Run Code Online (Sandbox Code Playgroud)