简体   繁体   English

栅栏未注册-Google感知API

[英]Fences not registering - google awareness API

I am currently registering a location fence on my application, similar to how it is explained on the documentation . 我目前正在我的应用程序上注册位置围栏,类似于文档中的解释。 Using logs, I can see the registering callback and the broadcast receiver are being correctly called. 使用日志,我可以看到正在正确地调用注册回调和广播接收器。 However, if I re-run the application, these are not fired. 但是,如果我重新运行该应用程序,则不会触发这些应用程序。 After rebooting my phone it works again. 重新启动手机后,它可以再次工作。

I am not unregistering the fence because I want the fence to be fired even in the background (the receiver is not tied to an Activity). 我之所以没有注销围栏,是因为我希望即使在后台也要触发围栏(接收者未绑定到“活动”)。

¿How can I get this working even if I re-run the application multiple times during application development? ¿即使我在应用程序开发期间多次重新运行应用程序,也如何使它正常工作? ¿How can I ensure the fence is correctly registered when a user reinstalls or updates the application? ¿当用户重新安装或更新应用程序时,如何确保围栏已正确注册?

I create the AwarenessFence using 我使用创建AwarenessFence

AwarenessFence allLocations = AwarenessFence.or(locationFences);

where locationFences is a collection of LocationFence objects created like this 其中locationFences是这样创建的LocationFence对象的集合

singleLocationFence = LocationFence.entering(latitude, longitude, FENCE_RADIUS); 

I've done the fence-handling in a service and since a service can run in background it works fine. 我已经在服务中完成了隔离处理,由于服务可以在后台运行,因此可以正常工作。 If "onDestroy" is called, I used to unregister the fences, so the operation-system will not have to observe these fences anymore. 如果调用了“ onDestroy”,则我通常会取消注册围栏,因此操作系统将不再需要观察这些围栏。 The service also solved your "rerun"-problem because it can only be on service at a time. 该服务还解决了您的“重新运行”问题,因为它一次只能使用。

Your next point with 您的下一点

AwarenessFence allLocations = AwarenessFence.or(locationFences);

its working, I tried it with a TimeFence 它的工作,我尝试与一个TimeFence

AwarenessFence allLocations = AwarenessFence.or(TimeFence.inInterval(new Date()), Long.MAX_VALUE));

But better use 但是更好的使用

AwarenessFence allLocations = locationFences;

since AwarenessFence is the parent of all BeaconFences, Geofences, TimeFencee, (...) form the Awareness API. 因为AwarenessFence是所有BeaconFence,Geofence,TimeFencee的父级,所以(...)组成Awareness API。

I understand that after you register fences with Awareness API, you'd like to receive callbacks even you re-run the app, or if the app is in the background. 我了解到,使用Awareness API注册篱笆后,即使您重新运行该应用程序,或者该应用程序在后台,您也希望收到回调。 The question does not clarify this but I believe if you're following the docs, you're registering the broadcastreceiver dynamically . 这个问题并不能澄清这一点,但是我相信,如果您关注文档,那么您是在动态注册broadcastreceiver。

You may achieve what you intend by doing static registration of your broadcast receiver by adding something like this in the manifest file : 您可以通过在清单文件中添加类似以下内容的静态注册广播接收器来实现您的预​​期目标:

<receiver android:name="MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
       </action>
    </intent-filter>
</receiver>

This way, you do not need to unregister the receiver in onDestroy(), and your app will get fence callbacks if you have registered fences, even if app is stopped or updated. 这样,您无需在onDestroy()中注销接收器,并且即使您已停止注册或更新应用程序,您的应用程序也将获得隔离器回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM