【问题标题】:Is there any notification that indicates iOS will put the device into a "sleep” state?是否有任何通知表明 iOS 会将设备置于“睡眠”状态?
【发布时间】:2014-06-24 08:29:24
【问题描述】:

我想知道iOS是否会向应用程序发送通知,指示系统会将设备置于“睡眠”状态。因为我希望我的应用程序在设备处于“睡眠”状态时执行一些操作。

【问题讨论】:

  • “睡眠”状态是什么意思?
  • 我认为“睡眠”=“锁定”屏幕;)
  • 是的,“睡眠”=“锁定”屏幕

标签: ios iphone


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    - (void)applicationWillResignActive:(UIApplication *)application

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

    【讨论】:

    【解决方案3】:

    不,唯一可以满足您需求的通知是UIApplicationWillResignActiveNotification,但它也会在发布时发布

    用户下拉通知中心

    闹钟响了

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

    【讨论】:

      【解决方案4】:

      好答案:

      我设法收到“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;
          }
      }
      

      枚举:

      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?

      【讨论】:

      • 也许我没有说清楚。我的意思是 UIApplicationState 是应用程序的状态,而不是设备的状态。即使我的应用程序已经在后台,设备仍然没有处于睡眠状态。
      • StateInactive(锁定)和StateBackground(只是应用休眠)的区别。
      • 我设法使用 CFNotificationCenterAddObserer 收到“com.apple.springboard.lockcomplete”通知。但是,如果我的应用程序在后台运行,它就无法收到通知。为了解决这个问题,我添加了 UIBackgroundModes(= audio) 来播放静音音频。最后,该应用程序运行良好。非常感谢!
      • 好的,我用你的答案编辑帖子;)或者你可以发布你的答案并验证它。因为您的问题仍未得到解答:/
      【解决方案5】:

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

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

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

      感谢大家的帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        相关资源
        最近更新 更多