gpt4 book ai didi

android - 不断添加接近警报,如何使用 id 删除它们?

转载 作者:行者123 更新时间:2023-11-29 21:47:45 25 4
gpt4 key购买 nike

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

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

第一次设置 map 时,alertid 为 0,有四个 map 标记,设置了四个接近警报,工作正常。当离开 map 并返回时再次设置,alertid 重置为 0,但警报再次添加,因此 8 个警报消失,每次添加 4 个新警报。我想通过将 alertid 重置为 0,再次重新创建它们会覆盖以前的,因为它们有一个 id,但这显然没有发生。任何人都可以看到它们是如何构建的,并且可以告诉我如何确保它们只为每个设置创建一次吗?

最佳答案

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

lm.removeProximityAlert(PendingIntent intent)

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

registerReceiver(new ProximityIntentReceiver(), filter);

关于android - 不断添加接近警报,如何使用 id 删除它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15323848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com