【问题标题】:Is there a away to detect the event when iOS device goes to sleep mode (when the screen gets blackened)?当iOS设备进入睡眠模式(屏幕变黑时)时,有没有办法检测到事件?
【发布时间】:2012-12-30 09:46:11
【问题描述】:

我想检测两个事件:

  1. 设备被锁定/解锁。
  2. 设备进入睡眠状态,屏幕变黑。

我在这里能够实现的第一个: Is there a way to check if the iOS device is locked/unlocked?

现在我想检测第二个事件,有什么办法吗?

【问题讨论】:

  • 这个问题是关于 iPhone 的私有 api 的吗?
  • 什么都行,我不打算在 App Store 上提交我的应用程序。

标签: ios iphone-privateapi sleep-mode


【解决方案1】:

您基本上已经有了解决方案,我猜您是从我最近的一个答案中找到的 :)

使用com.apple.springboard.hasBlankedScreen 事件。

当屏幕空白时会发生多个事件,但这一个就足够了:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                NULL, // observer
                                hasBlankedScreen, // callback
                                CFSTR("com.apple.springboard.hasBlankedScreen"), // event name
                                NULL, // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);

回调在哪里:

static void hasBlankedScreen(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    NSString* notifyName = (__bridge NSString*)name;
    // this check should really only be necessary if you reuse this one callback method
    //  for multiple Darwin notification events
    if ([notifyName isEqualToString:@"com.apple.springboard.hasBlankedScreen"]) {
       NSLog(@"screen has either gone dark, or been turned back on!");
    }
}

更新:正如@VictorRonin 在下面的评论中所说,应该很容易跟踪屏幕当前是打开还是关闭。这使您可以确定屏幕打开或关闭时是否正在发生hasBlankedScreen 事件。例如,当您的应用启动时,设置一个变量以指示屏幕处于打开状态。此外,任何时候发生任何 UI 交互(按下按钮等),您都知道当前必须打开屏幕。因此,您收到的下一个hasBlankedScreen 应该表明屏幕已关闭。

另外,我想确保我们清楚术语。当屏幕因超时自动变暗或用户手动按下电源按钮时,设备锁定。无论用户是否配置了密码,都会发生这种情况。届时,您将看到com.apple.springboard.hasBlankedScreen 和com.apple.springboard.lockcomplete 事件。

当屏幕重新打开时,您将再次看到com.apple.springboard.hasBlankedScreen。但是,在用户通过滑动(可能还有密码)实际解锁设备之前,您不会看到 com.apple.springboard.lockstate。


更新 2:

还有另一种方法可以做到这一点。您可以使用一组备用 API 来侦听此通知,也可以在通知到来时使用 get a state variable:

#import <notify.h>

int status = notify_register_dispatch("com.apple.springboard.hasBlankedScreen",
                                      &notifyToken,
                                      dispatch_get_main_queue(), ^(int t) {
                                          uint64_t state;
                                          int result = notify_get_state(notifyToken, &state);
                                          NSLog(@"lock state change = %llu", state);
                                          if (result != NOTIFY_STATUS_OK) {
                                              NSLog(@"notify_get_state() not returning NOTIFY_STATUS_OK");
                                          }
                                      });
if (status != NOTIFY_STATUS_OK) {
    NSLog(@"notify_register_dispatch() not returning NOTIFY_STATUS_OK");
}

您需要保留一个 ivar 或其他一些 persistent 变量来存储通知令牌(不要只在方法中将其设为局部变量)注册!)

int notifyToken;

您应该看到通过notify_get_state() 获得的state 变量在0 和1 之间切换,这可以让您区分屏幕开启和关闭事件。

尽管this document is very old,它确实列出了哪些通知事件具有可以通过notify_get_state()检索的关联状态。

警告: see this related question for some complications with this last technique

【讨论】:

  • 我已经尝试过了,事实证明,当设备进入睡眠状态和设备从睡眠中唤醒时,此通知都会出现在两种情况下。:1 月 16 日 18:48:53 未知 SpringBoard[15 ] : Posting 'com.apple.iokit.hid.displayStatus' notifyState=0 Jan 16 18:48:53 unknown Myapp[1921] : Darwin notification NAME = com.apple.springboard.hasBlankedScreen an 16 18 :50:29 unknown SpringBoard[15] : Posting 'com.apple.iokit.hid.displayStatus' notifyState=1 Jan 16 18:50:29 unknown Myapp[1921] : Darwin notification NAME = com. apple.springboard.hasBlankedScreen
  • 您可以保留一个状态变量,无论设备现在是睡眠还是唤醒。
  • 当锁定操作发生时,如果我的应用程序已经在后台,我是否正确地注意到它不会收到锁定状态或锁定完成通知?
  • @SpencerWilliams,不一定。如果您只是按 Home 来后台您的应用程序,假设您的应用程序 .plist 未指定它在挂起时退出,那么您将收到通知。现在,与任何事情一样,您的应用程序在后台运行的时间是有限制的,因此如果您想无限期地获得这些通知,您可能不得不摆弄一些事情。这真的是另一个问题,取决于我们是否在谈论越狱应用程序。但是,从根本上说,达尔文通知不会因为您进入后台而停止。
  • @SpencerWilliams,还要注意我在注册通知时传递的最后一个参数。如果您没有使用CFNotificationSuspensionBehaviorDeliverImmediately,那么当您的应用处于后台时,您很可能看不到通知。
【解决方案2】:

您还可以订阅通知:“com.apple.springboard.lockstate”并使用 API SBGetScreenLockStatus 来确定设备是否被锁定。

【讨论】:

  • 如何调用和使用?实际上我只想检测屏幕变黑时的事件,即屏幕从开/关转换。提前致谢。
  • 信息:不是越狱设备。
  • 第一个通知与 Nate 编写的相同代码一起使用。链接文章中描述了 SBGetScreenLockStatus。我添加这些事件作为“设备被锁定/解锁”的答案。它适用于非越狱设备。
  • @Everybody:请看这个问题,因为它解释了这种方法的一些潜在缺陷:stackoverflow.com/questions/14820249/…
猜你喜欢
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 1970-01-01
相关资源
最近更新 更多