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

18

我试图在后台收集超过10分钟的CoreMotion加速度数据。因为像Sleep Cycle这样的应用程序可以实现这一点,所以这一定是可行的。

但我只是想确保这是被允许的,因为它似乎不属于以下范围之一:

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来说有更好的方式:

Header:

UIBackgroundTaskIdentifier bgTask; 

代码:

// 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);
    }    
}); 
}
1个回答

19

使用这个:

Apps that keep users informed of their location at all times, such as a navigation app
换句话说,你创建一个位置管理器并告诉它开始更新。你不必对这些更新做任何操作!但只要这个过程在进行 - 即你的应用程序继续在后台进行位置更新 - 你的应用程序也被允许在后台使用核心运动。这不仅仅是一个技巧;正如几年前的一次WWDC视频所解释的那样,这是官方的苹果政策。

1
使用该解决方案,是否也可以在后台模式下获取有关用户步行/跑步/驾车的信息,并使用该信息启动位置更新,例如GPS? - verklixt
1
@verklixt 这没有任何意义。如果您的应用程序在后台并且在进入后台时尚未获取位置更新,则它将被挂起。因此,显然无法“开始”执行任何操作。(另外,在后台不需要获取核心运动活动更新;设备始终会为您获取它们。) - matt
3
@matt,你提到了一段WWDC视频。能否请你明确指出是哪一段? - Artem Stepanenko
2
这对电池寿命不好,我希望有另一种解决方案。 - Ramy Al Zuhouri
1
@MottiShneor,你解决了吗?我想在后台获取高达60Hz的传感器数据,但它只给我10Hz的数据。 - Reham M Samir
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接