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

查看:105
本文介绍了使用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);
                                                 }];

当应用程序处于前台模式时,我正在接收日志,但是当它进入后台时,只有当应用程序中的音乐正在播放时,我才会收到日志。
我已将以下内容添加到app info 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,

正如您所说的应用程序注册位置更新 ,在前台启动位置管理器。

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. 从外部配件接收定期更新的应用程序

  1. Apps that play audible content to the user while in the background, such as a music player app
  2. Apps that record audio content while in the background.
  3. Apps that keep users informed of their location at all times, such as a navigation app
  4. Apps that support Voice over Internet Protocol (VoIP)
  5. Apps that need to download and process new content regularly
  6. Apps that receive regular updates from external accessories

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

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