是否有任何通知表明iOS会将设备置于“睡眠”状态?

时间:2014-06-24 08:29:25

标签: ios iphone

我想知道iOS是否向应用发送通知,表明系统会将设备置于“睡眠”状态。因为我希望我的应用程序在设备进入" sleep"州。

5 个答案:

答案 0 :(得分:1)

答案是"否"。您只能跟踪应用程序状态而不是设备状态。

答案 1 :(得分:0)

好答案:

  

我设法收到" com.apple.springboard.lockcomplete"通知   使用CFNotificationCenterAddObserer。但是,如果我的应用程序正在运行   后台,它无法接收通知。为了解决这个问题,我   添加了UIBackgroundModes(= audio)以播放静音音频。在   最后,该应用程序运行良好。

索菲亚

通知:

   - (void)applicationDidEnterBackground:(UIApplication *)application {
        UIApplicationState state = [[UIApplication sharedApplication] applicationState];
        NSLog(@"Device state: %@", state);
        switch (state) {
            case UIApplicationStateActive:
                /* ... */
                break;

        case UIApplicationStateInactive:
            /* Device was/is locked  */
            break;
        case UIApplicationStateBackground:
            /* User pressed home button or opened another App (from an alert/email/etc) */
            break;
    }
}

Enum:

UIApplicationState - The running states of an application

typedef enum {
    UIApplicationStateActive,   
    UIApplicationStateInactive,
    UIApplicationStateBackground
}
UIApplicationState

Constants

UIApplicationStateActive - The application is running in the foreground and currently receiving events. Available in iOS 4.0 and later.

UIApplicationStateInactive - The application is running in the foreground but is not receiving events. This might happen as a result of an interruption or because the application is transitioning to or from the background.

UIApplicationStateBackground - The application is running in the background.

资料来源:Is it possible to distinguish between locking the device and sending an app to background?

答案 2 :(得分:0)

当设备进入非活动状态时,它会发送通知

- (void)applicationWillResignActive:(UIApplication *)application

此方法应在应用程序委托中实现。当进入非活动状态时,它将自动调用。它也会在您接到电话时调用

答案 3 :(得分:0)

不,唯一的通知可以满足您的需求UIApplicationWillResignActiveNotification,但也会在

时发布

用户下拉通知中心

闹钟响起来了

在警报视图中显示推送通知。

答案 4 :(得分:0)

" com.apple.springboard.lockcomplete"是我用来解决问题的通知。

接收" com.apple.springboard.lockcomplete"通知,我在applicationDidFinishLaunching中使用了CFNotificationCenterAddObserer。(据说私有api~)

但是,如果我的应用程序在后台运行,则无法接收通知。为了解决这个问题,我添加了UIBackgroundModes(= audio)来播放静音。最后,该应用程序运行良好。

感谢大家的帮助!