实施地理围栏 - 当应用程序未运行时接收地理围栏转换事件 运行

Implementing Geo-fencing - Receiving geo-fences transition events when the app is not running

我正在实施地理围栏。它运行良好,但它们的转换事件仅在应用程序处于后台或前台时才会触发。我有一个接收地理围栏转换事件的 IntentService,但我的问题是此 Intent 服务仅在应用程序为 运行 时才接收更新。

实现有一个标准的 LocationService,用于在 运行 时由应用程序管理地理围栏,然后是一个 IntentService,当检测到地理围栏转换时,它会收到 OS 的通知。

即使应用根本不是 运行,您是否有任何解决方案来接收转换?

谢谢。

Intent service 应该启动一个工作线程,处理您计划执行的任何操作,然后自行关闭。在您的情况下,当应用程序正在 运行 时,服务基本上会生成、工作然后自行关闭。

来自 android 文档

the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

要解决此问题,您可能需要 create your own service implementation via extending the Service class.

Checkout diff between Intent Service and Service

P.S :如果您提供了一些您已经尝试过的代码,那么当应用程序未 运行ning 时,可以直接向您展示您的代码 运行 到底是什么。

我根据 official documentation 找到了解决方案。

建议的最小半径应在 100150 米之间,特别是当您在位置请求中使用 PRIORITY_LOW_POWERPRIORITY_BALANCED_POWER_ACCURACY 时。

我遇到的问题是我将 geo-fences 保存到半径非常小(例如 20 米)的设备,在这种情况下设备没有检测到 geo-fence 转换事件当应用程序不是 运行(不在后台和前台)时根本不存在。