在 iOS 4 上很长时间后在后台执行简单的任务+
让我重新表述一下,使其更清楚:
如何在我的应用程序进入后台 20 分钟后发出“警报”?
老问题:
如何在 iOS 上 5-10-60 分钟后获得最简单的体验?本质上,在预定义的时间间隔后触发 NSTimer。
我“开始任务”,睡眠(一段时间);然后“结束任务”。然而,如果我睡觉超过 5 分钟,我的应用程序通常会被杀死。我看过一些帖子提到应用程序可以在后台保持活动状态几个小时,这究竟是如何实现的?
Let me rephrase to make it clearer:
How can I have an "alarm" go off 20 minutes after my app has gone in the background?
Old question:
How can you have the simplest tast happen after 5-10-60 minutes on iOS? Essentially, have an NSTimer fire after a predefined interval.
I do "begin task", sleep( for some time ); and then "end task". However my app usually gets killed if I sleep for more than 5 minutes. I've seen posts around that mention that apps can stay alive in the background for a few hours, how exactly can that be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您的应用程序进入后台时,系统将尝试重新收集您可以提供给他的尽可能多的资源,例如图像、笔尖等。您会收到相关通知。 (
-applicationDidEnterBackground:
等)之后,它会对所有后台生活应用程序按照资源使用顺序进行“排序”。使用更多资源的应用程序优先。
然后,当一个新应用程序进入前台时,如果系统认为它需要比当前拥有的更多资源,它将开始杀死后台应用程序。使用上面的排序列表。
这意味着您在后台使用的资源越少,您的应用程序的生存时间就越长。但是,不能保证它会长久存在。 (一切都取决于用户的使用情况)。当它被杀死时也没有任何通知!,所以你不能假设你的应用程序将存活 60 分钟。
您可能想阅读 这个 Apple 文档 关于如何在后台执行某些操作(当然有一些时间限制),可能还有 UIApplicationDelegate 类参考。
希望它有帮助;)
When your app goes to the background, the system will try to recollect as many as resources as you can give it to him like Images, nibs, etc. You have a notification for this. (
– applicationDidEnterBackground:
, etc)After that, it will "sort" all the background living apps according the resources usage order. The app that uses more resources comes first.
Then when a new app comes to the foreground if the systems thinks it needs more resources than it currently has it will start killing background apps. using the above sorted list.
This means that the less resources you use in the background, the longer your app will live. BUT, there is no guarantee it will live long. (Everything depends on user usage). There is also no notification when it gets killed!, so you cannot assume your app will live for 60 min.
You probably want to read this Apple doc on how to do something in the background (with some time restrictions, off-course) and probably also UIApplicationDelegate class reference.
Hope it helps ;)
只需使用本地通知
Just use a local notification