知道系统何时在 Menu Extra 中进入睡眠状态? [英] Knowing when the system has gone to sleep in a Menu Extra?

查看:53
本文介绍了知道系统何时在 Menu Extra 中进入睡眠状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I developed a Menu Extra (menulet) for the Mac but I'd like to know if the machine using the Menu Extra has gone to sleep. I implemented a snooze function but since it's a time-based method it's rather unreliable. Any hints or advice would be greatly appreciated!

谢谢!

推荐答案

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

You probably want to check out NSWorkspaceWillSleepNotification in NSWorkspace.
The trick is, you need to register for the notifications on NSWorkspace's notificationCenter, not the default one. See Technical Q&A 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];
}

这篇关于知道系统何时在 Menu Extra 中进入睡眠状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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