简体   繁体   English

当应用程序到后台IOS时,保持加速度计运行

[英]Keep accelerometer running when for app to background IOS

I am using the accelerometer in IOS and I want it to run in background mode. 我在IOS中使用加速度计,我希望它以后台模式运行。

I tried to use CMMotionManager like this but it didn't work: 我尝试使用像这样的CMMotionManager但它不起作用:

CMMotionManager*manager= [[CMMotionManager alloc] init];
if(!manager.accelerometerAvailable) {
    NSLog(@"Accelerometer not available");
} else {
    manager.accelerometerUpdateInterval = 0.1;
    NSOperationQueue *motionQueue = [[NSOperationQueue alloc] init];

    [manager startAccelerometerUpdatesToQueue: motionQueue withHandler:
     ^(CMAccelerometerData *data, NSError *error) {
         NSLog(@"Accelerometer data: %@", [data description]);
     }
     ];
}

How can I do this? 我怎样才能做到这一点?

Not sure this solves your problem, but a glance at the docs ( http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html ) says: 不确定这会解决您的问题,但只需浏览文档( http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html )即可:

"Implementing Long-Running Background Tasks “实施长期运行的后台任务

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: 在iOS中,只允许在后台运行特定的应用类型:

  1. Apps that play audible content to the user while in the background, such as a music player app 在后台播放用户可听内容的应用,例如音乐播放器应用

  2. Apps that keep users informed of their location at all times, such as a navigation app 随时向用户通知其位置的应用,例如导航应用

  3. Apps that support Voice over Internet Protocol (VoIP) 支持互联网协议语音(VoIP)的应用

  4. Newsstand apps that need to download and process new content 需要下载和处理新内容的报亭应用程序

  5. 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." 声明服务可以让系统知道您使用哪些服务,但在某些情况下,系统框架实际上会阻止您的应用程序被暂停。“

Not sure if you're trying to use the accelerometer to do any of these things (communicate with a blue tooth device perhaps?), but if so, you'll need to declare the services you support in the app. 不确定你是否正在尝试使用加速度计来做这些事情(或许可以与蓝牙设备通信?),但如果是这样,你需要在应用程序中声明你支持的服务。 To do that, you need to add the UIBackgroundModes key to your Info.plist and then add an array containing the relevant string(s) for the services you're trying to use. 为此,您需要将UIBackgroundModes键添加到Info.plist中,然后添加一个包含您尝试使用的服务的相关字符串的数组。 See the docs page linked above for the full list of the strings. 有关字符串的完整列表,请参阅上面链接的文档页面。

If you're not trying to use the accelerometer to do any of those things, it looks as if you may be out of luck. 如果你没有尝试使用加速计做任何这些事情,看起来好像你可能运气不好。 Though I'd love to be wrong there.. (anybody?) 虽然我喜欢那里错了..(有人吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios应用程序,在后台运行时运行加速度计 - ios app, running accelerometer while the app is in the background 是否可以让 flutter 应用程序在后台运行时不会被 iOS 杀死 - is it possible to keep flutter app do not killed by iOS when running in background 在iOS中对应用程序数据进行加密时,后台服务可以继续运行吗 - Can background services keep running when the App Data is encrypted in iOS iOS使我的应用程序不断在后台运行 - iOS keep my app running in background constantly 保持计数器在iOS应用程序中在后台运行 - Keep counter running in background on iOS app 在后台运行时访问iOS中的加速度计 - Accessing the accelerometer in iOS while running in the background 当应用程序在后台运行时,保持NSTimer运行 - Keep NSTimer running when app is in background 当应用程序进入后台时继续运行 NSOperationQueue - Keep running NSOperationQueue when app goes to background 当应用程序在后台运行时,迅速读取3个加速度计数据 - swift 3 read accelerometer data when app is in background 应用程序未运行时,iOS后台模式? - iOS background mode when app is not running?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM