我为 Mac 开发了一个 Menu Extra(menulet),但我想知道使用 Menu Extra 的机器是否已进入睡眠状态。我实现了一个贪睡功能,但由于它是基于时间的方法,因此相当不可靠。任何提示或建议将不胜感激!
Thanks!
您可能想查看NSWorkspaceWillSleepNotification
in NS工作区 http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html.
诀窍是,您需要在 NSWorkspace 的 notificationCenter 上注册通知,而不是默认的通知。请参阅技术问答 QA1340 http://developer.apple.com/library/mac/#qa/qa1340/_index.html.
示例代码:
- (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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)