-3

I am creating an app to add and remove proximity alerts. Proximity alert working fine. It does notifies me when I come close to the coordinates. But I can not remove them when I don't want them. Here is the code I've used to Add and remove proximity alerts.

When I stop and read them the number of alerts increments by one than the previous count.

Adding Proximity Alerts

private void add_lert() {
    locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
    String proximitys="com.realtech.proxtest";
    Intent i=new Intent(proximitys);
    pi = PendingIntent.getBroadcast(MainActivity.this, 11, i, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager.addProximityAlert(lat, lng, radius, -1, pi);
    IntentFilter filter=new IntentFilter(proximitys);
    registerReceiver(new ProxReceiver(),filter);
}

Removing Alerts

private void remove_alert() {
    LocationManager locationManager1=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    String proximitys="com.realtech.proxtest";
    Intent i=new Intent(proximitys);
    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 11, i, PendingIntent.FLAG_CANCEL_CURRENT);
    locationManager1.removeProximityAlert(pi);
}
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Praveen Kumar
  • 547
  • 1
  • 7
  • 33

1 Answers1

0

Have you tried to use global variables for PendingIntent instead declare a new one inside each block (add/remove).

Also the locationManager have a different name in the two blocks. Try to use a global variable for it as well.

Jdark0
  • 1