Android – 为什么要使用挂起意图进行地理围栏

我刚刚在 Android( http://developer.android.com/training/location/geofencing.html)上完成了地理围栏的教程,我想知道为什么地理围栏的’回调’是通过挂起的意图而不是简单的回调接口完成的.

如果在一个活动中实现,通常会在onPause()中断开位置客户端,因此在应用程序暂停/被销毁后,以前添加的地理围栏也不会被跟踪,那么为什么一个未决的意图呢?或者我错了?

解决方法

I wonder why the ‘callback’ for geofences are done via pending intents and not a simple callback interface.

主要是因为地理围栏设计为即使没有运行应用程序也可以工作.

If implemented in an activity,one would usually disconnect the location client in onPause() anyway,so prevIoUsly added geofences would not be tracked either after the application paused/was destroyed,so why a pending intent? Or am I mistaken here?

我相信你错了.实际上,地理围栏专门不是为直接触发UI而设计的,如the documentation中所述:

The Intent sent from Location Services can trigger varIoUs actions in your app,but you should not have it start an activity or fragment,because components should only become visible in response to a user action.

现在,您可以选择说您只想在前台活动时使用地理围栏.但是,您必须在onPause()中删除这些地理围栏.地理围栏将在到期时间之前保持注册状态或手动删除,AFAICT.

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...