【问题标题】:Proximity alerts keep being added, how to remove them by using id's?不断添加接近警报,如何使用 id 删除它们?
【发布时间】:2013-03-11 16:47:55
【问题描述】:

我为每个地图标记设置了我的地图和接近警报,这里我将经纬度和地名传递给添加接近警报方法:

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,但这显然不会发生。任何人都可以看到它们是如何构建的,也许可以告诉我如何确保它们只为每个设置创建一次?

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    我想您应该维护某种列表,在其中保留所有警报的意图,以避免将它们添加两次或多次。此外,根据您的使用场景,您可能应该在收到通知后将其删除:

    lm.removeProximityAlert(PendingIntent intent)
    

    但更有可能的是您多次注册了 BroadcastReceiver :) 尝试只调用一次(而不是每次添加警报时):

    registerReceiver(new ProximityIntentReceiver(), filter);
    

    【讨论】:

    • 我不确定如何删除接近警报,您能看看我的添加接近警报方法并告诉我创建删除方法的代码吗?我试了两个星期没有运气:(谢谢!
    • 老实说,如果我重新考虑正确,我在尝试删除proximityAlerts 时也遇到了很多问题。尝试将您的 PendingIntents 保存在一个列表(例如 ArrayList)中,以便以后能够再次删除它们。
    • 好的,我会试一试,这是您所做的并且有效吗?谢谢!
    • 抱歉,记不清了。但这家伙似乎解决了问题:stackoverflow.com/questions/9761033/…
    • 是的,我一直在关注这个问题,并与 OP 取得了联系!干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多