1

マップと各マップマーカーの近接アラートを設定しました。ここでは、緯度と経度を渡し、名前を近接アラートの追加メソッドに配置します。

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であり、4つのマップマーカーがあり、4つの近接アラートがセットアップされ、正常に機能します。マップを離れてマップを戻すと、alertidは0にリセットされますが、アラートが再度追加されるため、8つのアラートがオフになり、毎回4つの新しいアラートが追加されます。alertidを0にリセットして、再度作成するとIDがあるため、以前のものが上書きされると思いましたが、これは明らかに発生していません。誰かがそれらがどのように構築されているかを見ることができますか、そして多分それらがすべてのセットアップに対して一度だけ作成されることを確実にする方法を私に教えてくれますか?

4

1 に答える 1

2

アラートを2回または数回追加しないように、すべてのアラートをその意図とともに保持する、ある種のリストを維持する必要があると思います。また、使用シナリオによっては、通知後にそれらを削除する必要があります。

lm.removeProximityAlert(PendingIntent intent)

ただし、BroadcastReceiverを数回登録した可能性があります:)これを1回だけ呼び出すようにしてください(アラートを追加するたびではありません)。

registerReceiver(new ProximityIntentReceiver(), filter);
于 2013-03-10T15:36:04.107 に答える