2013-05-08 80 views
0

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

  1. 采用螺纹
  2. 当应用程序中去捕捉背景时间和回来

我不知道哪种方式更好?如果我使用线程,那么它的工作时间有多长?它在后台工作几个小时?

谢谢

回答

0

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

你可以像运行后台任务:

... 
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不允许运行app无限时间。请参阅BackgroundExecution演示以供参考。

+0

这是否意味着我不会在背景中运行一个任务(计时器在我的情况下)超过10分钟。 – User382 2013-05-08 17:35:10

+0

你可以做到这一点,但'iOS'不保证该应用程序保持活着。 – 2013-05-08 18:07:44

+0

另一种方式是注册应用程序,它可能会执行一些特殊的操作,如使用VOIP服务播放背景音频。参考文献:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW3,paragraph'Implementing Long-运行后台任务'。 – 2013-05-08 18:16:19

相关问题