36

With the M7 chip in the latest iOS devices one can get programmatically notified as the user goes from stationary to running, walking, etc using CMMotionActivityManager. Stava and Runkeeper have both used this to auto-pause GPS polling (shut off the GPS antenna) when it detects the user isn't moving via the M7, and then re-enable GPS updates once they are moving again. It is able to do this while the app is in the background state, which is the key here.

The issue I run into while duplicating this functionality is that if I turn off GPS updates while my app is in the background I stop receiving activity updates, and can no longer detect when the user moves again via the M7 to turn the GPS back on.

If I leave the GPS running the whole time I'll continue to get movement updates from Core Motion the entire time the app is in the background.

I'm assuming they aren't playing white-noise or some other cheap trick to stay active. How did they go about this?

4

5 回答 5

17

RunKeeper 实际上确实使用音频技巧来保持清醒。如果您打开应用程序包并检查其 Info.plist,您将看到它注册了背景音频模式。这就是他们如何定期发送有关您的距离、速度和步速的音频通知。这也是它们如何在您的跑步过程中保持清醒,同时最大限度地减少电池消耗。

如果您注意到在使用 RunKeeper 时位置服务图标(状态栏中的三角形)完全消失了,那么他们肯定没有使用任何类型的位置跟踪来完成后台执行。即使激活地理围栏和重要的位置变化监控也会导致位置服务图标出现。

他们也没有使用 M7 来保持清醒,因为它不是那样工作的。来自 M7 相关 CoreMotion API 的更新不会将您的应用从睡眠中唤醒。当他们的应用程序确实醒来时,他们将能够查询运动活动和步数历史,并可能尝试计算一些东西,但我怀疑它会那么准确。

最后,您应该注意 Auto-pause API 是在 iPhone 5s 和 M7 芯片发布之前的 iOS 6 中引入的。它们是正交的概念。

于 2014-03-21T01:37:59.097 回答
0

我注意到当你关闭 GPS 时,应用程序不会在 iOS 7 的后台执行任何代码,应用程序看起来像处于非活动状态。所以在转移到后台使用时会更好,startMonitoringSignificantLocationChanges并且还可以从您的位置管理器那里获得更新。意味着startUpdatingLocation在用户状态更改和startMonitoringSignificantLocationChanges后台同时使用服务。

因此,当用户打开 GPS 时,您使用startMonitoringSignificantLocationChanges的应用程序将收到

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

最好在后台检查一下 CoreMotion 框架有什么问题。并尝试重新启动它。因为没有 M7 芯片设备,我可以在这种情况下读取加速度计读数。

于 2013-12-09T13:30:11.407 回答
0

你有没有考虑过尝试

application:performFetchWithCompletionHandler: 

在应用程序委托中?您无法控制调用的频率,但根据应用程序的不同,可以每约 15 分钟调用一次。然后,您可以从那里启动 CMMotionActivityManager 以查询 M7 结果。

目前尚不清楚您要复制什么功能,但 M7 芯片会记录所有活动,无论您的应用程序是否正在运行。因此,您可以简单地在后台查询并更新步骤总计或活动类型总计。

于 2014-04-08T22:04:46.640 回答
0

如果您的位置管理器在活动模式下工作,要启用后台模式,您需要执行以下三个步骤:

  1. 检查[目标/功能/背景模式/位置更新]是否已启用。
  2. [locationManager requestAlwaysAuthorization];
  3. locationManager.allowsBackgroundLocationUpdates = YES;
于 2016-04-20T17:51:19.307 回答
-2

首先,检查您是否设置了应用程序的后台行为。

转到目标 - 功能部分并检查后台模式以获取位置更新。

XCode 功能

于 2014-01-30T13:36:40.560 回答