【问题标题】:Receive accelerometer updates in background using CoreMotion framework使用 CoreMotion 框架在后台接收加速度计更新
【发布时间】:2014-02-18 06:55:18
【问题描述】:

我正在使用以下代码获取 accelerometer 数据(使用CoreMotion 框架):

CMMotionManager *motionManager = [[CMMotionManager alloc] init];    
motionManager.accelerometerUpdateInterval = 1.0 / 60.0;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
                                                 withHandler:^(CMAccelerometerData  *accelerometerData, NSError *error) {
                                                 NSLog(@"ACCELEROMETER DATA = %@",accelerometerData);
                                                 }];

当应用程序处于前台模式时,我正在接收日志,但是当它进入后台时,我仅在应用程序中播放音乐时才收到日志。 我已将以下内容添加到应用信息 plist 文件中:

- Required background modes
   - App registers for location updates
   - App plays audio or streams audio/video using AirPlay

问题是:如何在不播放音乐的情况下在后台接收加速度计更新?

【问题讨论】:

  • 你不能,加速度计不是允许的背景模式之一,只能播放音乐、GPS 更新、VOIP 通话等。如果您添加背景模式但不使用它们,我认为它不会起作用。

标签: ios objective-c accelerometer core-motion


【解决方案1】:

您不仅可以使用加速度计从后台获取数据,

正如你所说的 App registers for location updates ,在前台启动位置管理器。

实现长时间运行的后台任务

对于需要更多执行时间来执行的任务,您必须请求特定权限才能在后台运行它们而不会被暂停。在 iOS 中,只允许特定的应用类型在后台运行:

  1. 在后台向用户播放有声内容的应用,例如音乐播放器应用
  2. 在后台录制音频内容的应用程序。
  3. 让用户随时了解其位置的应用,例如导航应用
  4. 支持互联网协议语音 (VoIP) 的应用
  5. 需要定期下载和处理新内容的应用
  6. 从外部附件接收定期更新的应用

【讨论】:

【解决方案2】:

在您的 AppDelegate.m 中

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data,     invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    UIApplication* app = [UIApplication sharedApplication];

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

【讨论】:

    猜你喜欢
    • 2014-03-23
    • 2014-01-10
    • 2017-07-03
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多