问题描述
我有一个计时器应用程序,它使用 NSTimer 来跟踪分钟.即使设备进入睡眠模式,我如何保持计时器运行?谢谢!
I have a timer app which uses a NSTimer to track minutes. How can I keep the timer going, even when the device is put into sleep mode? Thanks!
推荐答案
在唤醒通知后调整
预定的 NSTimer 实例将在睡眠期间暂停.您的应用程序需要响应 NSWorkspace 的唤醒通知并调整其计时器.
Adjust After Wake Notification
Scheduled NSTimer instances will halt during sleep. Your application needs to respond to the NSWorkspace's wake notification and adjust its timers.
如果您不响应唤醒通知,您的计时器将恢复,但不会考虑睡眠时间.
If you do not respond to wake notifications, your timers will resume but without accounting for the time spent asleep.
请参阅 Apple 的技术说明 QA1340 注册和注销睡眠和唤醒通知以获取适当的通知.
See Apple's technical note QA1340 Registering and unregistering for sleep and wake notifications for getting the appropriate notifications.
考虑这种情况:
- 创建一个在 5 分钟内触发的计时器
- 等一分钟
- 让计算机休眠一个小时
- 唤醒计算机
- 定时器在醒来时恢复
- 定时器在醒来 4 分钟后触发
从计时器的角度来看,五分钟过去了.注意 NSTimer 使用的语言,它使用相对持续时间而不是绝对时间,scheduledTimerWithTimeInterval:invocation:repeats:
.
From the timer's perspective, five minutes has passed by. Note the language used by NSTimer, it uses relative durations rather than absolute times, scheduledTimerWithTimeInterval:invocation:repeats:
.
这篇关于NSTimer 在睡眠模式下继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!