如何使用 Cocoa 列表在 Mac 进入睡眠状态之前获得通知?

问题描述

我正在开发一个 Java 应用程序,我需要使用以下代码在它休眠之前接收休眠通知。

以下是 Apple Developer's site 建议的用于在睡眠前接收睡眠通知的代码,我需要将此代码与我的 java 应用程序集成才能这样做。

- (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];

}

代码在我看来就像没有头没有尾的东西,我觉得很难理解。即使有人能解释这段代码是如何单独工作的,那也会让我受益匪浅。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)