Android的 - 为什么要使用挂起的意图进行geofences要使、意图、挂起、Android

2023-09-06 02:42:33 作者:便秘时想起你

我刚刚完成了教程地理围栏在Android(http://developer.android.com/training/location/geofencing.html)我想知道为什么回调的geofences通过悬而未决的意图进行,而不是一个简单的回调接口。

如果在活动中实现的,人们通常会断开在的onPause的位置客户端()无论如何,所以previously加入geofences不会跟踪任何申请后已暂停/被破坏了,那么为什么挂起的意图是什么?还是我在这里弄错了?

解决方案   

我不知道为什么在回调的geofences通过未决的意图进行,而不是一个简单的回调接口。

这主要是因为geofences的设计,即使不工作,应用程序运行。

  

如果在活动中实现的,人们通常会在断开的的onPause位置客户端(),无论如何,所以previously加入geofences不会跟踪任何应用程序暂停后/被破坏了,那么为什么挂起的意图是什么?还是我在这里弄错了?

我相信你在这里错了。事实上,geofences特别不用于直接触发UI中,如在的文档:

  

从位置服务发送的意图可以触发各种动作在你的应用程序,但你不应该有它开始一个活动或片段,因为组件应该只在响应用户的操作变得可见。

华硕ROG新款手机发布,0流量接收信息,与苹果APNS 谷歌FCM相当

现在,你可能会选择说你只想使用geofences,而你有你在前台活动。但是,你必须删除这些geofences在的onPause()。地理栅栏将继续注册,直到它的到期时间或手动删除,AFAICT。

I just finished the tutorial for geofencing on Android (http://developer.android.com/training/location/geofencing.html) and 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?

解决方案

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

Mostly because geofences are designed to work even without your application running.

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?

I believe that you are mistaken here. In fact, geofences specifically are not designed for directly triggering UI, as is discussed in 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.

Now, you might elect to say that you want to only use geofences while you have your activity in the foreground. However, you would have to remove those geofences in onPause(). A geofence will remain registered until its expiration time or manually removed, AFAICT.