【问题标题】:MonoTouch running NSTimer even when app is in background即使应用程序在后台运行,MonoTouch 也会运行 NSTimer
【发布时间】:2013-05-08 16:12:24
【问题描述】:

在我的应用程序中,即使应用程序进入后台,我也想运行计时器。我想有两种方法可以做到这一点

  1. 使用线程
  2. 捕捉应用进入后台和返回的时间

我不知道哪种方式更好?如果我使用线程,那么它可以工作多长时间?它可以在后台工作数小时吗?

谢谢

【问题讨论】:

    标签: background xamarin.ios nstimer


    【解决方案1】:

    如果您的应用进入后台状态,您没有太多时间停止当前线程。

    你可以像这样运行后台任务:

    ...
    public partial class AppDelegate : UIApplicationDelegate
    {
        ...
        public override void DidEnterBackground (UIApplication application)
        {
            int taskID = UIApplication.SharedApplication.BeginBackgroundTask ( () => {});
            var task = new Thread (new ThreadStart (() => { 
                // Save application's current state.
            }));
            task.Start ();
        }
    

    因此,您将拥有最多 10 minutes

    然而iOS 不允许应用程序无限运行时间。参考BackgroundExecutiondemo。

    【讨论】:

    • 这是否意味着我无法在后台运行任务(在我的情况下为计时器)超过 10 分钟。
    • 您可以这样做,但iOS 不保证该应用程序仍然存在。
    • 另一种方式是注册app,它可能会做一些特殊的操作,比如playing background audiousing VOIP services。参考:developer.apple.com/library/ios/#documentation/iphone/…,段落Implementing Long-Running Background Tasks
    • 为了更好地理解 iOS 中的多任务处理和后台工作,我建议阅读本书 amazon.co.uk/Developing-Apps-iPhone-using-MonoTouch/dp/… 中的相关章节 11 "Multitasking"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    相关资源
    最近更新 更多