是否可以知道 iPhone 何时进入睡眠状态?

发布于 2024-09-13 15:46:30 字数 906 浏览 1 评论 0原文

是否有任何通知可以提醒我手机将进入睡眠状态?我已经实现了以下内容:

[[NSNotificationCenter defaultCenter] 
              addObserver:self 
                 selector:@selector(resignActive:)
                     name:UIApplicationDidEnterBackgroundNotification
                   object:nil];

但当手机在一段时间不活动后(在我的设置中为 1 分钟)进入睡眠状态时,它似乎不会被触发。我认为它不起作用的原因是我有一个计时器,每秒执行一个操作,如果经过了一定时间(大于一分钟),它会发出音频警报。在我的 resignActive 中,我使计时器无效,当我按下主页按钮时,计时器工作正常,但当手机进入睡眠状态时,计时器就不起作用。它似乎仍在后台运行,但“速度较低”,因为时间比正常情况长得多(大约 10 分钟而不是 2 分钟)。

你知道手机进入睡眠状态时会发生什么吗?我读过这两篇文章,但它并没有真正回答我的问题。

iPhone 应用会发生什么情况iPhone何时进入待机模式?

当 iPhone 处于睡眠模式时,iPhone 应用程序延迟 10 -15 分钟

Is there any notification I can listen to that will alert me that the phone will go to sleep? I have implemented the following:

[[NSNotificationCenter defaultCenter] 
              addObserver:self 
                 selector:@selector(resignActive:)
                     name:UIApplicationDidEnterBackgroundNotification
                   object:nil];

but it seems that it's not getting triggered when the phone goes to sleep after some time of inactivity (1 min in my settings). The reason I think it's not working is that I have a timer that perform an action every second and if a certain time has passed (bigger than one minute) it issues an audio alarm. In my resignActive I invalidate my timer and that works fine when I press the home button, but not when the phone goes to sleep. It still seems to be running in the background but "at a lower speed" as the times are much longer than normal (around 10 min instead of 2 min).

Any ideas what's happening when the phone goes to sleep? I have read these two posts but it doesn't really answer my question.

What happens to an iPhone app when iPhone goes into stand-by mode?

Iphone app is delayed for 10 -15 minutes when iphone is in sleep mode

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-09-20 15:46:30

在 UIApplicationDelegate 上实现这 2 个方法:

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application

当显示来自另一个应用程序(或 SMS)的推送通知时也会调用它......

Implements those 2 methods on the UIApplicationDelegate :

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application

It will be called also when a push notification from another app (or a SMS) is shown...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文