在 OS X 上系统空闲或不空闲时的通知 [英] Notification when the system is idle or not on OS X

查看:20
本文介绍了在 OS X 上系统空闲或不空闲时的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 OS X 上使用 IOKit 框架可以通过某种方式获得系统空闲时间,但我想知道是否有可用的通知.

I know that there is some way to get the system idle time with the IOKit framework on OS X, but I want to know if there's notifications available.

我可以创建一个计时器来检查空闲时间是否超过 x,这很好.如果我在几秒钟后检测到空闲模式也没关系.

I can create a timer to check if the idle time is more than x, and that's fine. It doesn't matter if I detect the idle mode a few seconds later.

问题是检测 Mac 何时不再空闲.我希望我的应用程序尽快显示通知,而不是几秒钟后.

The problem is to detect when the Mac is not idle anymore. I want my app to show a notification as soon as possible, not a few seconds later.

有没有办法收到通知?(iChat 好像有)

Is there a way to have a notification for that? (iChat seems to have one)

推荐答案

本文来自 http://developer.apple.com/library/mac/#qa/qa1340/_index.html(来自比尔蜥蜴评论)

This is from http://developer.apple.com/library/mac/#qa/qa1340/_index.html (from Bill the Lizard comment)

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

- (void) receiveWakeNote: (NSNotification*) note
{
    NSLog(@"receiveWakeNote: %@", [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: NULL];

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
            selector: @selector(receiveWakeNote:) 
            name: NSWorkspaceDidWakeNotification object: NULL];
}

这篇关于在 OS X 上系统空闲或不空闲时的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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