2013-03-10 38 views
1

設置我的地圖,每個地圖標記接近的警報,在這裏我傳遞的緯度和長,地名的增加接近警報方法:接近警報不斷被添加,如何通過使用ID的刪除它們?

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,但這顯然不會發生。任何人都可以看到他們是如何建立的,也許告訴我如何確保他們只爲每個設置創建一次?

回答

2

我想你應該保持某種類型的列表,在那裏你保留所有的警報與他們的意圖,以避免添加他們兩次或幾次。同時根據您的使用情況下,你應該將其刪除,通知後:

lm.removeProximityAlert(PendingIntent intent) 

但更可能的是,你註冊的廣播接收器幾次:) 儘量只用一次這個功能(而不是每次添加時間一個警報):

registerReceiver(new ProximityIntentReceiver(), filter); 
+0

我不確定如何刪除接近警報,你能看看我的添加接近警報方法,並告訴我的代碼來創建一個刪除方法嗎?我試了兩個星期沒有運氣:(謝謝! – deucalion0 2013-03-10 15:43:34

+0

說實話,當我嘗試刪除proximityAlerts時,我也遇到了很多問題,如果我重新考慮正確的話。嘗試將你的PendingIntents保存在列表中(例如ArrayList)能夠將它們再次移除 – usb79 2013-03-11 17:11:18

+0

好的,我會試試看,是你做了什麼,它的工作?謝謝! – deucalion0 2013-03-12 16:11:56

相關問題