2011-06-20 62 views
10

Tôi biết rằng có một số cách để hệ thống không hoạt động với khung IOKit trên OS X, nhưng tôi muốn biết liệu có thông báo nào không.Thông báo khi hệ thống không hoạt động hoặc không có trên OS X

Tôi có thể tạo bộ hẹn giờ để kiểm tra xem thời gian rảnh có lớn hơn x hay không và điều đó tốt. Nó không quan trọng nếu tôi phát hiện chế độ nhàn rỗi một vài giây sau đó.

Sự cố là để phát hiện khi máy Mac không hoạt động nữa. Tôi muốn ứng dụng của mình hiển thị thông báo sớm nhất có thể, không phải vài giây sau đó.

Có cách nào để có thông báo cho điều đó không? (IChat dường như có một)

+3

Tôi đã thử nghiệm 'NSWorkspaceScreensDidWakeNotification' và nó hoạt động cung cấp các định nghĩa về trở về từ nhàn rỗi là giống như thức dậy màn hình. Ngoài ra, bạn có thể phải cài đặt một lần nhấn sự kiện để phát hiện các sự kiện chuột/bàn phím, diễn giải chúng khi trở về từ chế độ chờ. –

+1

@Bavarious làm cho câu trả lời này là một câu trả lời. – batman

+1

Tôi không biết nếu có sự khác biệt giữa Cocoa và Objective C, nhưng câu trả lời cho [Mục tiêu C: Nhận thông báo về trạng thái không sử dụng của người dùng] (http://stackoverflow.com/questions/4643579/objective-c-get -notifications-about-a-user-nhàn rỗi-state) có thể hữu ích. –

Trả lời

8

Đây là từ http://developer.apple.com/library/mac/#qa/qa1340/_index.html (từ Bill nhận xét Lizard)

- (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]; 
} 
1
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; 
} 
Các vấn đề liên quan