I set up my map and a proximity alert for each map marker, here I am passing the lat and long, and place name to the add proximity alert method:
if(alerts==true)
{
addProximityAlert(l1, l2, place);
}
The add proximity alert method:
//The following sets up proximity alerts, getting a unique id for each one
private void addProximityAlert(Double latitude, Double longitude, String tit) {
Intent intent = new Intent(PROX_ALERT_INTENT);
intent.putExtra("name", tit);
intent.putExtra("id", alertid);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, alertid, intent, PendingIntent.FLAG_ONE_SHOT);
lm.addProximityAlert(latitude, longitude, POINT_RADIUS, PROX_ALERT_EXPIRATION,proximityIntent );
alertid++;
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT );
registerReceiver(new ProximityIntentReceiver(), filter);
}
The following is the proximity alert class:
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}else {
Log.d(getClass().getSimpleName(), "exiting");
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, Map.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = createNotification();
notification.setLatestEventInfo(context, "Proximity Alert!", "You are approaching: " +intent.getStringExtra("name"), pendingIntent);
notificationManager.notify( intent.getIntExtra("id", -1), notification);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.icon = R.drawable.ic_launcher;
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = Color.CYAN;
notification.ledOnMS = 15000;
notification.ledOffMS = 15000;
return notification;
}
}
The first time the map is setup alertid is 0, and there are four map markers, and four proximity alerts are setup and it works fine. When leaving the map and returning it is setup again, alertid is reset to 0, but the alerts are added again, so 8 alerts go off, 4 new alerts added every time. I thought by resetting alertid to 0, recreating them again would overwrite the previous ones as they have an id, but this is obviously not happening. Can anyone see how they are building up, and maybe show me how to ensure they are only created once for every setup?
I guess you should maintain some kind of List where you keep all your Alerts with their intents to avoid adding them twice or several times. Also depending on your usage scenario, you should probably remove them, after notification:
But more probable is that you registered your BroadcastReceiver several times :) Try to call this only once (and not every time you add an alert):