iPhone 应用程序处于非活动状态时 NSTimer 回调
这个问题似乎是这个论坛上其他几个问题的本质。 我相信活动的iPhone应用程序有可能继续运行,特别是在进入非活动状态后继续接收计时器回调(通过屏幕锁定中的空闲计时器,或者通过用户按下硬件锁定按钮)。
该文档特别指出,当应用程序处于非活动状态时,它正在执行,但不会分派传入事件(我没有提供链接,因为我对 NDA 很紧张 - 我应该对此放松吗?这整篇文章是否违规?< em>叹息)。
另外,用户“Ambr Str”的两个答案直接指出可以继续接收计时器回调,并且他提供了一段代码来实现它(我无法链接到此,因为我是新用户,抱歉 - 搜索问题:“当 iPhone 进入待机模式时,iPhone 应用程序会发生什么情况?”以找到答案)。
我尝试按照他的建议创建回调,但是一旦我的应用程序变得不活动,回调就会停止触发。
我刚刚注意到,当 iPhone 插入时,如果应用程序变得不活动(由于空闲超时或我按下睡眠按钮),回拨确实会继续发生 - 也许我应该让我的用户携带电池和他们一起打包;-)
This question seems to be the essence of several others on this forum. I believe that it is possible for the active iPhone application to continue running, and specifically, to continue receiving timer call-backs, after it has entered the inactive state (either through the idle timer kicking in the screen lock, or through the user pressing the hardware lock button).
The documentation specifically says that while an application is inactive, it is executing, but not dispatching incoming events (I'm not giving a link because I'm jumpy about NDA - should I relax about that? Is this whole post a breach? sigh).
Also, two answers by user "Ambr Str" directly state that it is possible to continue receiving timer call-backs, and he supplies a snip of code to achieve it (I can't link to this because I'm a new user, sorry - search for the question: "What happens to an iPhone app when iPhone goes into stand-by mode?" to find his answer).
I've tried to create my call-backs as he suggests, but once my application becomes inactive, the call-backs cease firing.
I've just noticed that while the iPhone is plugged in, if the application becomes inactive (due to idle time out or me pressing the sleep button), call-backs do continue to occur - perhaps I should get my users to carry a battery pack with them ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题在苹果论坛上有一个很好的答案。 搜索“Timer”和“Eskimo”(提供答案的乐于助人的小伙子)。
简而言之,应用程序变为非活动状态后不久,手机确实会进入睡眠状态。 防止这种情况的唯一方法是播放一些音频(或在后台应用程序中播放一些音频)。 播放音频时,手机不会休眠,您的应用程序将继续运行。
建议播放保持清醒的音频是一种黑客行为,如果可能的话,您不应该这样做。 我认为在我的应用程序(执行穿插有静默期的音频播放)中,该方法即使不理想,也是有效的!
There is a good answer to this question on Apple's forums. Search for "Timer" and "Eskimo" (the helpful chap who provides the answers).
In brief, shortly after the application becomes inactive, the phone really does go to sleep. The only way to prevent this is to be playing some audio (or for some audio to be playing in a background application). While audio is playing the phone will not sleep, and your application continues to be run.
It is suggested that playing stay awake audio is a hack, and that you shouldn't do it if at all possible. I think in my application (which performs audio playback interspersed with silent periods), the approach is valid, if not ideal!
我有一个应用程序。 其中包括一个嵌入式网络服务器。 我计划提供一个禁用自动睡眠的选项,前提是服务器已打开,并且设备已通电。 因此,您可能会考虑检查 UIDevice 的 BatteryState 属性。 因此,如果 BatteryState != UIDeviceBatteryStateUnplugged,请继续并禁用空闲计时器。 (注意:文档说,在模拟器中时将返回 UIDeviceBatteryStateUnknown。)
您还需要侦听 UIDeviceBatteryStateUnplugged 通知并适当设置 BatteryMonitoringEnabled。
I have an app. which includes an embedded webserver. I'm planning to offer an option of disabling auto-sleep IFF the server is turned on, AND the device is on power. So you might consider checking the batteryState property of UIDevice. So if batteryState != UIDeviceBatteryStateUnplugged, go ahead and disable idle timer. (note: the docs say that UIDeviceBatteryStateUnknown will be returned when in simulator.)
You'll also want to listen for UIDeviceBatteryStateUnplugged notifications and set batteryMonitoringEnabled appropriately.