终止/暂停重大更改位置API的行为? [英] Behaviour for significant change location API when terminated/suspended?

查看:81
本文介绍了终止/暂停重大更改位置API的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是 CLLocationManager <的部分/ a>使用 startMonitoringSignificantLocationChanges


如果你启动这项服务和你的
申请随后
终止,系统自动
如果新事件到达,则将应用程序重新启动到
后台。在
这样的情况下,选项字典
传递给
应用程序:didFinishLaunchingWithOptions:
应用程序委托方法
包含密钥
UIApplicationLaunchOptionsLocationKey
表示您的应用程序因位置事件而启动

重新启动后,您仍必须
配置位置管理器对象
并调用此方法继续
接收位置事件。当您
重新启动位置服务时,当前的
事件会立即传递给您的代理
。此外,即使在
开始位置服务之前,您的位置管理器
对象的位置
属性也会填充最多
的最近位置对象。

If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the application:didFinishLaunchingWithOptions: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.

所以我的理解是,如果你的应用程序终止(我假设你不打电话给 applicationWillTerminate )你将被唤醒 UIApplicationLaunchOptionsLocationKey 参数 application:didFinishLaunchingWithOptions 。此时,您可以创建 CLLocationManager ,调用 startMonitoringSignificantLocationChanges 并为时间有限。所以我对此感觉不错。

So my understanding is that if your app terminates (and I assume if you don't call stopMonitoringSignificantLocationChanges from applicationWillTerminate) you will get woken up with a UIApplicationLaunchOptionsLocationKey parameter to application:didFinishLaunchingWithOptions. At that point you create your CLLocationManager, call startMonitoringSignificantLocationChanges and do your background location processing for a limited time. So I am fine with this bit.

上一段仅讨论应用程序终止时会发生什么,它不会建议您在应用程序暂停时执行的操作。 didFinishLaunchingWithOptions 说:

The previous paragraph only talks about what happens when the app is terminated, it doesn't suggest what you do when the application is suspended. The documentation for didFinishLaunchingWithOptions says:


应用程序在后台跟踪位置
更新,清除,
,现已重新启动。在这个
的情况下,字典包含一个键
,表示由于新的位置
事件,该应用程序重新启动了

The application tracks location updates in the background, was purged, and has now been relaunched. In this case, the dictionary contains a key indicating that the application was relaunched because of a new location event.

建议您在终止后启动应用程序(因为位置更改)时才会收到此电话。

Suggesting that you will only receive this call when your app is launched (because of a location change) after you have been terminated.

然而重大变更服务。 htmlrel =noreferrer>位置感知编程指南有以下说法:

However the paragraph on the Significant Change Service in the Location Awareness Programming Guide has the following to say:


如果你让这个服务保持运行并且
您的申请随后被暂停或终止
,服务
当新的位置数据
到达时,b会自动唤醒您的
申请。在唤醒时,您的
应用程序将被放入后台
并给予少量时间
处理位置数据。因为
你的应用程序在后台,
它应该做最小的工作并避免
任何可能阻止
返回的任务(例如查询
网络)在分配的时间
到期之前。如果没有,您的
申请可能会被终止。

If you leave this service running and your application is subsequently suspended or terminated, the service automatically wakes up your application when new location data arrives. At wake-up time, your application is put into the background and given a small amount of time to process the location data. Because your application is in the background, it should do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your application may be terminated.

这表明如果你的应用程序你被唤醒了位置数据已被暂停,但没有提及你是如何被唤醒的:

This suggests you are woken up with location data if your app has been suspended, but fails to mention how you are woken up:

  • Does the UIApplicationDelegate get a callback telling me that I am resuming from a suspended state into a background state?
  • Does the location manager (that was freeze dried when the app was suspended) start receiving locationManager:didUpdateToLocation:fromLocation callbacks?
  • Do I just need to implement code in my didUpdateToLocation message which checks the application state and does minimal processing if in background mode?

在写这篇文章的过程中,我想我可能刚刚回答了我自己的问题,但是拥有我的问题会很棒。由更有见识的人证实了这一点。

In the process of writing this up, I think I may have just answered my own question, but it would be great to have my understanding of this confirmed by someone more knowledgeable.

推荐答案

由于我问了这个问题,我做了一些测试(主要是在家庭和工作之间的火车上)并确认暂停的应用程序的行为是我在问题的最后所怀疑的。

Since I asked this question, I have done a fair bit of testing (mostly on the train between home and work) and have confirmed that the behaviour for suspended apps is as I suspected at the end of the question.

也就是说,您的被暂停的应用程序被唤醒,您的应用代表没有收到任何回调,而是通过您现有的 CLLocationManagerDelegate 。您可以通过检查 applicationState 来检测您在后台运行,并针对您从暂停状态唤醒以进行位置处理的情况进行有限的工作。

That is, your suspended app is woken up, you don't receive any callbacks on your app delegate, instead you receive your location updates through your existing CLLocationManagerDelegate. You can detect that you are running in the background by checking the applicationState, and do limited work for the case where you are woken from a suspended state to do location processing.

[UIApplication sharedApplication].applicationState == UIApplicationStateBackground

我通过位置测试工具得出了这个结论,欢迎您下载并尝试。这是一个非常简单的应用程序,允许您通过用户界面打开重大更改和GPS更改API,并记录您收到的所有响应。

I came to this conclusion with a location test harness that you are welcome to download and try out. It is a pretty simple app that allows you to turn on significant change and GPS change APIs through the UI and log all the responses that you get back.

N.B。上一个答案中的第六点不正确。 冻结暂停的应用程序在从暂停状态唤醒时会收到 CLLocationManagerDelegate 回调。

N.B. Point six in the previous answer is not correct. Freeze dried suspended apps do receive CLLocationManagerDelegate callbacks when they are woken up from a suspended state.

这篇关于终止/暂停重大更改位置API的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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