简体   繁体   English

移除和重新添加后,接近警报触发两次

[英]Proximity alert firing twice after removing and re-adding

I am having a problem with the proximity alert. 我有接近警报的问题。 I have settings on my android application where users can disable/enable proximity alert of some locations. 我在我的Android应用程序上有设置,用户可以禁用/启用某些位置的近距离警报。 When they disable the proximity alert everything works great and they will not be notified, but if they disable and the re-enable the proximity alert, it adds again and they will get a notification twice when they reach a location and so on. 当他们禁用接近警报时,一切都很好,他们将不会得到通知,但如果他们禁用并重新启用接近警报,它会再次添加,当他们到达某个位置时他们会收到两次通知,依此类推。 So basically every time they re-enable it, it creates a new proximity alert. 所以基本上每次重新启用它时,它都会创建一个新的接近警报。

This is the code I use to remove the proximity alert: 这是我用来删除近距离警报的代码:

private void removeProximityAlert(int id) {
    final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    Intent intent = new Intent(PROX_ALERT_INTENT + id);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    manager.removeProximityAlert(proximityIntent);
}

And this is the code that I use to add a proximity alert: 这是我用来添加接近警报的代码:

private void addProximityAlert(double latitude, double longitude, int id, int radius, String title) {
    final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
    Intent intent = new Intent(PROX_ALERT_INTENT + id);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    manager.addProximityAlert(
           latitude,
           longitude,
           radius,
           -1,
           proximityIntent
    );

    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id);
    registerReceiver(new ProximityIntentReceiver(id, title), filter);
}

My understanding of FLAG_CANCEL_CURRENT is that it tells the system that the old pending intent is no longer valid and it should cancel it and then create a fresh one. 我对FLAG_CANCEL_CURRENT理解是它告诉系统旧的待处理意图不再有效,它应该取消它然后创建一个新的意图。 Correct me if I am wrong. 如果我错了,请纠正我。 This, I believe, is why you are duplicating Proximity Alerts with each cancel and creation. 我相信,这就是为什么每次取消和创建都要复制Proximity Alerts的原因。

Resolution: 解析度:

In your removeProximityAlert I would change the following line 在你的removeProximityAlert我会更改以下行

PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);

to: 至:

PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT).cancel();

FLAG_UPDATE_CURRENT returns the existing one created, if any. FLAG_UPDATE_CURRENT返回创建的现有文件(如果有)。 cancel() should take care of the rest for you. cancel()应该为你处理剩下的事情。


Edit 编辑

If I break the cancel() into a separate line I don't get any errors. 如果我将cancel()分成一个单独的行,我不会得到任何错误。 Try this: 尝试这个:

PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
proximityIntent.cancel();

The problem is that you're registering the receiver each time you add the alarm. 问题是每次添加警报时都要注册接收器。 You should register it only once. 你应该只注册一次。

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

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