• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps how to remove proximity alerts?

robothito

Newbie
I am building an app that uses Location Manager and proximity alerts. No problem with this.
Say I add a proximity alert
Code:
locationManager.addProximityAlert(latitude,
                        longitude,
                        radius,
                        -1,
                        pendingIntent);

How do I remove it later?
Now you may say "use removerProximityAlert". Fair enough.
But this function needs the pending intent that the alert was created with so I can do that if my activity is on the foreground and I have that intent.
Maybe (I am not sure really-can I?) I can do that even if the activity goes to background so it pauses and restart again.. - is the pending intent still there?

But what about if the activity gets destroyed? - Say I change the tablet orientation.
Then the intent is gone so how can I remove that proximity alert?
 
Thanks for the reply.
For example I have in the ProximityAlertActivity class:
Java:
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_proximity_alert);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
       requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION_REQUEST_CODE);

        pendingIntent =
PendingIntent.getBroadcast(this,0,new Intent(PROXIMITY_ACTION),PendingIntent.FLAG_UPDATE_CURRENT);

        preferences = getPreferences(MODE_PRIVATE);

        setProximityAlert = (Button) findViewById(R.id.setProximityAlert);
        clearProximityAlert = (Button) findViewById(R.id.clearProximityAlert);

    }

As you see there I get the location Manager and get a pending intent
then in setProximityAlert
Java:
 public void onSetProximityAlertClick(View view)
    {
//checkPermission()
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {
    locationManager.addProximityAlert(latitude,
            longitude,
            radius,
            -1,
            pendingIntent);
}
//some other code
}

there I add the proximity alert
Also I have
Java:
 public void onClearProximityAlertClick(View view)
    {
    
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {

            locationManager.removeProximityAlert(pendingIntent);
            }
      
        setProximityAlert.setEnabled(true);
        clearProximityAlert.setEnabled(false);
    }

Now you see, there I can remove the proximity alert because I still have the pending intent.

But what if the activity gets paused? (maybe after restarting the intent would be there?) but what if it gets destroyed???
Then how can I destroy the alert that I set originally??
 
I checked today and onStop gets called not only when the activity is destroyed but also when it is paused, so it does not help me much.
I read once that onDestroy is not guaranteed to be called, but let's suppose it is. So let's say I put remove proximity alert there.

The problem comes when I run the application. Picture it for a moment. I start the application, set the latitute and longitude, then set the proximity alert and then oops, for some reason I move the tablet or phone and the orientation gets changed. When this happen the application gets destroyed and started again and my proximity alert is gone!
Which is not what I want...

The ideal way would be some way that I can remove the alert some how explicitly even after the activity is destroyed
Is there some way to do this?
 
Back
Top Bottom