0

好的,我没有得到关于这个的答案。:(

Multipeer Connectivity 音频流停止在后台工作

那这个呢?

我正在尝试在后台运行此代码。

- (void)applicationDidEnterBackground:(UIApplication *)application {

    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

       [application endBackgroundTask:bgTask];
       bgTask = UIBackgroundTaskInvalid;
    }];


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSTimer *aTimer = [NSTimer timerWithTimeInterval:3
                                                  target:self
                                                selector:@selector(showInformation)
                                                userInfo:nil 
                                                 repeats:YES];

        [[NSRunLoop mainRunLoop]
           addTimer:aTimer
           forMode:NSDefaultRunLoopMode];

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

}

显然我在同一范围内定义了这个函数

- (void) showInformation {
  NSLog(@"showInformation is called.");
}

但是当我把应用程序放在后台时,间隔消息,停止工作,当我回到前台时继续工作........

这意味着不是在后台运行?

这可能吗?还是我试图做一些愚蠢的不可能的事情?

我真的很感激一些帮助。

非常感谢。

4

1 回答 1

1

无论您的代码是否有效,您的后台任务都会在一段时间(>10 分钟)后被 iOS 终止,除非您的应用程序中设置了 UIBackgroundModes(VOIP、定位服务、音频 ..)。

有关后台执行的更多信息,请查看后台执行和多任务处理

iOS7 中的另一个选项是使用Background Fetch,但您无法控制时间(iOS 使用了一个智能计时器)。

为了更好地理解,请查看 iOS 教程中的 Raywenderlich 的背景模式

如果您需要一些工作,请查看以下 SO 帖子:

如何在我的 iOS 应用程序中每 n 分钟更新一次后台位置?

在后台运行 iOS App 超过 10 分钟

如果用户强制退出,iOS 会将我的应用程序启动到后台吗?

于 2014-02-20T07:43:52.213 回答