【问题标题】:i want to terminate the app after some minutes while it run in background我想在后台运行几分钟后终止应用程序
【发布时间】:2011-08-03 08:45:25
【问题描述】:

我想在应用程序在后台运行几分钟后终止它。

这样的步骤:

按下主页按钮,然后应用程序在后台运行。 2 分钟后应用程序自动终止。

我找到了如下方法:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.

           //============================
          // here  i want to add a timer  
          //=============================  

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

如你所见, 我想在那个地方添加一个计时器。

但是计时器在后台没有工作。我也尝试了 performSelector:afterdelay:

我该怎么做?

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案1】:

    10 分钟后,所有应用程序都会终止。

    如果您必须在 2 分钟后退出应用程序,

    在 (applicationWillResignActive) 中设置计时器

    【讨论】:

      【解决方案2】:

      我想您的应用正在后台执行某种工作 - 否则一开始就不需要在后台运行它。

      我进一步假设您正在进行某种 runloop,因为您希望在一定时间后终止,而不是在某些任务完成后终止。

      因此,我建议在应用程序进入后台时记录系统时间,然后在您的运行循环中每次迭代检查一次当前系统时间。如果大于初始时间 + 2 分钟,则终止后台活动。

      【讨论】:

      • 对不起。你的反对是错误的......事实上,在后台没有任何作用。我只是想要一个可以在后台做一些工作的任务。但是,当应用程序返回时,什么都不能工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多