简体   繁体   English

检测 iPhone 屏幕是否打开/关闭

[英]Detect if iPhone screen is on/off

Is there any way to detect if an iPhone's screen is on or off?有什么方法可以检测 iPhone 的屏幕是打开还是关闭? For example, when the phone's screen lock button is pressed.例如,当按下手机的屏幕锁定按钮时。

I've been using (void)applicationWillResignActive:(UIApplication *)application;我一直在使用(void)applicationWillResignActive:(UIApplication *)application; to prepare for such events (which works fine for the most part), but this method is also fired for incoming calls, texts, etc.为此类事件做准备(在大多数情况下都可以正常工作),但此方法也会为来电、文本等触发。

As far as I can tell, there is no documented method to determine this.据我所知,没有记录的方法来确定这一点。

I've been playing with some workarounds, like checking if screen resolution changed, checking if the orientation is unknown, or getting the brightness of the device.我一直在尝试一些变通方法,例如检查屏幕分辨率是否改变、检查方向是否未知或获取设备的亮度。 Nothing has panned out yet.还没有任何结果。

Does anyone have any creative/workaround solutions for this?有没有人对此有任何创造性/解决方案?

Yes, there is no definitive method.是的,没有确定的方法。 UIApplication has a property protectedDataAvailable which will return YES when screen is unlocked and NO if locked only when user enables content protection . UIApplication有一个属性protectedDataAvailable ,它在屏幕解锁时返回YES ,如果仅在用户启用内容保护时锁定返回NO So this is the closest but unreliable I can think of.所以这是我能想到的最接近但不可靠的。 In such case, you can even listen to UIApplicationProtectedDataDidBecomeAvailable and UIApplicationProtectedDataWillBecomeUnavailable notifications.在这种情况下,您甚至可以收听UIApplicationProtectedDataDidBecomeAvailableUIApplicationProtectedDataWillBecomeUnavailable通知。

You can use Darwin notifications , to listen for the events.您可以使用Darwin 通知来监听事件。 I'm not 100% sure, but it looks to me, from running on a jailbroken iOS 5.0.1 iPhone 4, that one of these events might be what you need:我不是 100% 确定,但在我看来,在越狱的 iOS 5.0.1 iPhone 4 上运行,这些事件之一可能是您需要的:

com.apple.iokit.hid.displayStatus
com.apple.springboard.hasBlankedScreen
com.apple.springboard.lockstate

Note: according to the poster's comments to a similar question I answered here , this should work on a non-jailbroken phone, too.注意:根据发布者对我在这里回答的类似问题的评论,这也应该适用于未越狱的手机。

To use this, register for the event like this (this registers for just one event, but if that doesn't work for you, try the other two):要使用它,请像这样注册事件(这仅注册一个事件,但如果这对您不起作用,请尝试其他两个):

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                NULL, // observer
                                displayStatusChanged, // callback
                                CFSTR("com.apple.iokit.hid.displayStatus"), // event name
                                NULL, // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);

where displayStatusChanged is your event callback:其中displayStatusChanged是您的事件回调:

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSLog(@"event received!");
    // you might try inspecting the `userInfo` dictionary, to see 
    //  if it contains any useful info
    if (userInfo != nil) {
        CFShow(userInfo);
    }
}

I believe the events I listed above get triggered when the screen is both turned on and off, locked and unlocked.我相信我上面列出的事件会在屏幕打开关闭、锁定和解锁时触发。 You may need to track the state yourself.您可能需要自己跟踪 state。 Also,还,

com.apple.springboard.lockcomplete

is only called when the screen locks, not when it unlocks.仅在屏幕锁定时调用,而不是在解锁时调用。

Try this workaround.试试这个解决方法。 Author claims that it works well on 4.2作者声称它在 4.2 上运行良好

I have checked it on iOS 3.1 (iPhone 3G) - works well.我已经在 iOS 3.1 (iPhone 3G) 上检查过它——效果很好。

update: Doesn't work on iOS 5 beta 7 (iPod Touch 4G):-(更新:不适用于 iOS 5 beta 7 (iPod Touch 4G):-(

update2: app go to background when screen locked, thus solution is kinda working on iOS 5 beta 7:-) update2:当屏幕锁定时,应用程序 go 到后台,因此解决方案有点适用于 iOS 5 beta 7:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM