【问题标题】:iOS accelerometer in the background (infinite)iOS 加速度计在后台(无限)
【发布时间】:2013-06-11 11:27:17
【问题描述】:

我希望我的应用可以无限期地监控我的加速度计(即使在后台)。当达到一定数量时,我想发送本地通知。这与“睡眠周期”应用的功能完全相同。

但是他们是怎么做到的呢?当我使用 beginBackgroundTaskWithExpirationHandler 函数时,它在 10 分钟后不再工作。添加 UIBackgroundModes 根本没有帮助。

UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

_motionManager = [[CMMotionManager alloc] init];
_motionManager.accelerometerUpdateInterval = 1;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[_motionManager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
 {
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
         NSLog(@"%@",accelerometerData);
             if(accelerometerData.acceleration.x > 0.5){

                 UILocalNotification *localNotif = [[UILocalNotification alloc] init];

                 if (localNotif == nil)
                     return;

                 localNotif.fireDate = [NSDate date];
                 localNotif.timeZone = [NSTimeZone defaultTimeZone];

                 localNotif.alertBody = @"Wake up!";
                 localNotif.alertAction = @"wake";

                 localNotif.soundName = UILocalNotificationDefaultSoundName;
                 localNotif.applicationIconBadgeNumber = 4;

                 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
                 [app endBackgroundTask:bgTask];
                 bgTask = UIBackgroundTaskInvalid;
             }
     });        
 }];

【问题讨论】:

  • 当前 SDK 无法做到这一点。
  • 这不是真的,因为itunes.apple.com/nl/app/sleep-cycle-alarm-clock/… 正是这样做的。
  • 你有这个应用程序吗?使用时,应用程序需要处于活动状态,前台。
  • 是的,我有,它在后台运行:sleepcycle.com/faq/…
  • 抱歉,没有使用4.0版本,但是红条会显示,说明他们使用了API方法错误。听起来像录音。

标签: iphone ios background accelerometer multitasking


【解决方案1】:
猜你喜欢
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 2014-03-05
相关资源
最近更新 更多