【发布时间】:2014-06-24 08:29:24
【问题描述】:
我想知道iOS是否会向应用程序发送通知,指示系统会将设备置于“睡眠”状态。因为我希望我的应用程序在设备处于“睡眠”状态时执行一些操作。
【问题讨论】:
-
“睡眠”状态是什么意思?
-
我认为“睡眠”=“锁定”屏幕;)
-
是的,“睡眠”=“锁定”屏幕
我想知道iOS是否会向应用程序发送通知,指示系统会将设备置于“睡眠”状态。因为我希望我的应用程序在设备处于“睡眠”状态时执行一些操作。
【问题讨论】:
答案是“不”。您只能跟踪应用程序状态而不是设备状态。
【讨论】:
当设备进入非活动状态时,它会发送通知
- (void)applicationWillResignActive:(UIApplication *)application
此方法应在应用程序委托中实现。当 do 进入非活动状态时,它将自动调用。当你接到电话时它也会调用
【讨论】:
不,唯一可以满足您需求的通知是UIApplicationWillResignActiveNotification,但它也会在发布时发布
用户下拉通知中心
闹钟响了
在警报视图中显示的推送通知。
【讨论】:
好答案:
我设法收到“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?
【讨论】:
“com.apple.springboard.lockcomplete”是我用来解决问题的通知。
为了接收“com.apple.springboard.lockcomplete”通知,我在applicationDidFinishLaunching中使用了CFNotificationCenterAddObserer。(据说是私有api~)
但是,如果我的应用程序在后台运行,它就无法收到通知。为了解决这个问题,我添加了 UIBackgroundModes(= audio) 来播放静音音频。最后,应用运行良好。
感谢大家的帮助!
【讨论】: