我为每个地图标记设置了我的地图和接近警报,在这里我将经纬度和地名传递给添加接近警报方法:
if(alerts==true)
{
addProximityAlert(l1, l2, place);
}
添加接近警报方法:
//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);
}
以下是接近警报类:
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;
}
}
第一次设置地图alertid为0,有四个地图标记,设置了四个接近警报,效果很好。离开地图并重新设置时,alertid重置为0,但再次添加警报,因此8个警报消失,每次添加4个新警报。我认为通过将 alertid 重置为 0,再次重新创建它们会覆盖以前的,因为它们有一个 id,但这显然不会发生。谁能看到他们是如何建立的,也许可以告诉我如何确保每次设置只创建一次?