接近警报不断被加入,如何使用的ID删除它们? [英] Proximity alerts keep being added, how to remove them by using id's?

查看:135
本文介绍了接近警报不断被加入,如何使用的ID删除它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置我的地图,每个地图标记接近警报,在这里我传递lat和长,地名的增加接近警报方式:

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;
     }
}

第一次地图设置alertid为0,有四个地图标记,以及四个接近警报是安装和正常工作。当离开地图,并返回它的设置一遍,alertid被重置为0,但警报再次增加,因此8警报熄灭,4个新警报添加每次。我想通过重置alertid为0,再重新创建他们,因为他们有一个id将覆盖previous的人,但是这显然不会发生。任何人都可以看到他们是如何建立,也许告诉我如何确保他们只为每个设置创建一次?

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:

lm.removeProximityAlert(PendingIntent intent)

但更可能的是,你注册的广播接收器几次:)
尝试一次调用这个(而不是每次添加提醒时间):

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):

registerReceiver(new ProximityIntentReceiver(), filter);

这篇关于接近警报不断被加入,如何使用的ID删除它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆