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

时间:2021-03-01 00:04:48

Is there any way to detect if an iPhone's screen is on or off? For example, when the phone's screen lock button is pressed.

有没有办法检测iPhone的屏幕是打开还是关闭?例如,按下手机的屏幕锁定按钮。

I've been using (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.

我一直在使用(void)applicationWillResignActive :( UIApplication *)应用程序;为这些事件做准备(大部分工作都很好),但是对于来电,短信等也会触发这种方法。

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?

有没有人有这方面的创意/解决方案?

3 个解决方案

#1


9  

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. So this is the closest but unreliable I can think of. In such case, you can even listen to UIApplicationProtectedDataDidBecomeAvailable and UIApplicationProtectedDataWillBecomeUnavailable notifications.

是的,没有确定的方法。 UIApplication有一个属性protectedDataAvailable,它在屏幕解锁时返回YES,如果用户启用内容保护则返回NO。所以这是我能想到的最接近但不可靠的。在这种情况下,您甚至可以收听UIApplicationProtectedDataDidBecomeAvailable和UIApplicationProtectedDataWillBecomeUnavailable通知。

#2


2  

You can use Darwin notifications, to listen for the events. 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:

您可以使用Darwin通知来侦听事件。我不是百分百肯定,但它看起来像是在越狱的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. Also,

我相信当屏幕打开和关闭,锁定和解锁时,我上面列出的事件都会被触发。您可能需要自己跟踪状态。也,

com.apple.springboard.lockcomplete

is only called when the screen locks, not when it unlocks.

仅在屏幕锁定时调用,而不是在屏幕锁定时调用。

#3


1  

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:app在屏幕锁定时转到后台,因此解决方案有点适用于iOS 5 beta 7 :-)

#1


9  

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. So this is the closest but unreliable I can think of. In such case, you can even listen to UIApplicationProtectedDataDidBecomeAvailable and UIApplicationProtectedDataWillBecomeUnavailable notifications.

是的,没有确定的方法。 UIApplication有一个属性protectedDataAvailable,它在屏幕解锁时返回YES,如果用户启用内容保护则返回NO。所以这是我能想到的最接近但不可靠的。在这种情况下,您甚至可以收听UIApplicationProtectedDataDidBecomeAvailable和UIApplicationProtectedDataWillBecomeUnavailable通知。

#2


2  

You can use Darwin notifications, to listen for the events. 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:

您可以使用Darwin通知来侦听事件。我不是百分百肯定,但它看起来像是在越狱的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. Also,

我相信当屏幕打开和关闭,锁定和解锁时,我上面列出的事件都会被触发。您可能需要自己跟踪状态。也,

com.apple.springboard.lockcomplete

is only called when the screen locks, not when it unlocks.

仅在屏幕锁定时调用,而不是在屏幕锁定时调用。

#3


1  

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:app在屏幕锁定时转到后台,因此解决方案有点适用于iOS 5 beta 7 :-)