如何增加Android的区域范围设定的一致性进入/退出的通知?范围、区域、通知、Android

2023-09-04 07:53:11 作者:只為ㄋ遇見你

我使用的是内置的地理围栏的API(播放服务),并一直有不同的结果。它看起来像设置地理栅栏后,进入/退出的通知是很不一致,甚至在GPS上,有一个上最新的位置(LocationClient连接,在后台运行)。我开始监控(轮询)的位置变化,距离在调试文本字段,和看到,基于由装置中注册的位置和地理栅栏我技术上内/地理栅栏以外的位置即使当,通知有时触发并且有时没有。任何方式使这更predictable?我几乎想基于设备位置的投票放弃这个API和实现我自己的(电池电量耗尽)geofences。

I am using the built in geofence APIs (play services) and have been having mixed results. It looks like after setting a geofence, the notifications for entering/exiting are very inconsistent even when GPS is on, with an up-to-date location (LocationClient connected, running in the background.) I started monitoring (polling) location changes and distances in a debug text field, and saw that, even when based on the location registered by the device and the location of the geofence I am technically inside/outside of the geofence, notifications are sometimes triggered and sometimes not. Any way to make this more predictable? I am almost tempted to abandon this api and implement my own (battery draining) geofences based on polling of device location.

推荐答案

一对夫妇的建议:

请确保您使用一个BroadcastReceiver,而不是一个服务接收转换,否则你不会/可能不会得到它,如果你的应用程序就会被杀死/关闭。由于这里讨论: Android地理栅栏最终停止得到转变的意图

Make sure you're using a BroadcastReceiver and not a Service to receive the transitions, otherwise you will not/might not get it if your app gets killed/turned off. As discussed here: Android Geofence eventually stop getting transition intents

请确保您重新创建geofences设备重新启动后,preferably使用启动广播接收机。由于这里讨论: Do Geofences设备重启后继续活跃在Android的

Make sure you recreate your geofences after the device is rebooted, preferably using a boot-broadcast receiver. As discussed here: Do Geofences remain active in android after a device reboot

另外一个常见的​​误解,并且难倒我,因为它不同于IOS的是,你永远得到一个立即触发新创建地理围栏,如果设备发现您在创建它的时候是地理栅栏内。我已经使用了宽限期为新创建的geofences,这是我在这个线程解释解决了这个自己: addProximityAlert如预期

One other common misconception, and which stumped me since it's different than IOS is that you'll always get a trigger immediately for a newly created geofence, if the device discovers that you're inside the geofence when creating it. I have solved this myself using a "grace period" for newly created geofences, which i explained in this thread: addProximityAlert doesn't work as expected

最后一件重要的事情:有您LocationClient连接在你的应用程序或不不应该在所有问题,如果您按照上面的点。我的过程我code添加或删除Geofences基本上是:

Finally one important thing: Having your LocationClient connected in your app or not should not matter at all if you follow the points above. My process for adding or removing Geofences in my code is basically:

创建和连接locationclient。 Google 努力了三年,终于让 Android 手机的升级速度变快了些

Create and connect locationclient.

在连接回调,不要添加/删除地理围栏的(S)

In connect-callback, do adding/removing of geofence(s)

在地理栅栏,结果回调,断开locationclient。

In geofence-result callback, disconnect locationclient.

每次发生这种情况,我的位置客户端仅在总连接几秒钟。操作系统仍然会产生地理围栏警报,并打电话给我的BroadcastReceiver,每当他们发生了。

Every time this happens, my location client is only connected a few seconds in total. The operating system will still produce geofence alerts and call my BroadcastReceiver whenever they happen.

如果你做这些事情,我敢打赌,你的经验将得到改善。

If you do these things I bet your experience will improve.

希望这有助于!