2011-05-22 86 views
4

我爲Mac開發了一個Menu Extra(menulet),但我想知道使用Menu Extra的機器是否已經入睡。我實現了一個貪睡功能,但由於它是基於時間的方法,所以它相當不可靠。任何提示或建議將不勝感激!瞭解系統何時進入Menu Extra睡眠狀態?

謝謝!

回答

6

您可能想要查看NSWorkspaceWillSleepNotificationNSWorkspace
訣竅是,您需要在NSWorkspace的notificationCenter上註冊通知,而不是默認的通知。 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]; 
} 
相關問題