首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone在后台收集CoreMotion数据。(超过10分钟)

iPhone在后台收集CoreMotion数据。(超过10分钟)
EN

Stack Overflow用户
提问于 2013-12-24 19:54:47
回答 1查看 8.9K关注 0票数 18

我试图收集coreMotion加速数据在后台超过10分钟。这必须是可能的,因为应用程序,如睡眠周期,这样做。

不过,我只想确保这是允许的,因为它似乎并不是其中之一:

代码语言:javascript
复制
Apps that play audible content to the user while in the background, such as a music player app
Apps that record audio content while in the background.
Apps that keep users informed of their location at all times, such as a navigation app
Apps that support Voice over Internet Protocol (VoIP)
Apps that need to download and process new content regularly
Apps that receive regular updates from external accessories
Apps that implement these services must declare the services they support and use system frameworks to implement the relevant aspects of those services. Declaring the services lets the system know which services you use, but in some cases it is the system frameworks that actually prevent your application from being suspended.

然而,我已经尝试了以下步骤来获得一个背景任务,但是我认为CoreMotion有一个更好的方法:

标题:

代码语言:javascript
复制
UIBackgroundTaskIdentifier bgTask; 

代码:

代码语言:javascript
复制
// if the iOS device allows background execution,
// this Handler will be called
- (void)backgroundHandler {

NSLog(@"### -->VOIP backgrounding callback");

UIApplication*    app = [UIApplication sharedApplication];

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

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

while (1) {
    NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
       [self doSomething];
    sleep(1);
}   
});     

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

BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
if (backgroundAccepted)
{
    NSLog(@"VOIP backgrounding accepted");
}

UIApplication*    app = [UIApplication sharedApplication];

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


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

    while (1) {
        NSLog(@"BGTime left: %f", [UIApplication sharedApplication].backgroundTimeRemaining);
       [self doSomething];
       sleep(1);
    }    
}); 
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-24 20:08:50

用这个:

代码语言:javascript
复制
Apps that keep users informed of their location at all times, such as a navigation app

换句话说,您可以创建一个位置管理器,并告诉它开始进行更新。你不需要对那些更新做任何事情!但只要这种情况发生--也就是说,只要你的应用程序继续在后台进行位置更新--你的应用程序也可以在后台使用核心运动。这不仅仅是一个骗局,正如几年前WWDC的一段视频所解释的,这是苹果的官方政策。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20766139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档