我遇到了接近警报的问题。我在我的 android 应用程序上有设置,用户可以在其中禁用/启用某些位置的接近警报。当他们禁用接近警报时,一切正常,他们不会收到通知,但如果他们禁用并重新启用接近警报,它会再次添加,当他们到达某个位置时,他们会收到两次通知,依此类推。所以基本上每次他们重新启用它时,它都会创建一个新的接近警报。
这是我用来删除接近警报的代码:
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);
}
这是我用来添加接近警报的代码:
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);
}