gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 08:15:40 27 4
gpt4 key购买 nike

我试图在后台收集超过 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 有更好的方法:

标题:

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);
}
});
}

最佳答案

使用这个:

Apps that keep users informed of their location at all times, such as a navigation app

换句话说,您创建了一个位置管理器并告诉它开始进行更新。您无需对这些更新执行任何操作!但只要这种情况发生——也就是说,只要你的应用程序继续在后台进行位置更新——你的应用程序也被允许在后台使用 Core Motion。这不仅仅是一个技巧;正如几年前的一个 WWDC 视频中所解释的那样,这是 Apple 的官方政策。

关于ios - iPhone 在后台收集 CoreMotion 数据。 (超过10分钟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com