如果在一个活动中实现,通常会在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.