使用 CoreMotion 框架在后台接收加速度计更新 [英] Receive accelerometer updates in background using CoreMotion framework

查看:23
本文介绍了使用 CoreMotion 框架在后台接收加速度计更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码获取加速度计数据(使用CoreMotion框架):

I'm using the following code to get accelerometer data (using CoreMotion framework):

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 文件中:

When the app is in foreground mode, I'm receiving the log, but when it enters background, I receive the log only when the music in the app is playing. I've added the following to app info plist file:

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

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

The question is: how can I receive accelerometer updates in background, when the music is not playing?

推荐答案

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

You can not only use accelerometer for fetching data from background,

正如你所说的,你的 App 注册位置更新,在前台启动位置管理器.

And as you say your App registers for location updates , start location manager in the foreground.

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

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

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

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

这篇关于使用 CoreMotion 框架在后台接收加速度计更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆