Уведомление, когда система находится в режиме ожидания или нет в OS X
Я знаю, что есть способ получить время простоя системы с инфраструктурой IOKit в OS X, но я хочу знать, доступны ли уведомления.
Я могу создать таймер, чтобы проверить, является ли время простоя больше, чем х, и это прекрасно. Не имеет значения, если через несколько секунд я обнаружу режим ожидания.
Проблема заключается в том, чтобы обнаружить, когда Mac больше не праздный. Я хочу, чтобы мое приложение показывало уведомление как можно скорее, а не через несколько секунд.
Есть ли способ получить уведомление для этого? (iChat, похоже, один)
Ответы
Ответ 1
Это от http://developer.apple.com/library/mac/#qa/qa1340/_index.html (из комментария Билла Ящера)
- (void) receiveSleepNote: (NSNotification*) note
{
NSLog(@"receiveSleepNote: %@", [note name]);
}
- (void) receiveWakeNote: (NSNotification*) note
{
NSLog(@"receiveWakeNote: %@", [note name]);
}
- (void) fileNotifications
{
//These notifications are filed on NSWorkspace 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];
}
Ответ 2
NSTimeInterval GetIdleTimeInterval() {
io_iterator_t iter = 0;
int64_t nanoseconds = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj)
CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds);
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return (double)nanoseconds / 1000000000.0;
}