KansaiRobot
KansaiRobot

Reputation: 9912

How to check for Proximity Alerts and find them

I have a question regarding proximity alerts. In all tutorials I ve read they are created and destroyed while the activity that create them is still running. But what happens if say an activity creates n proximity alerts and then the activity itself is destroyed (the PA are not)

Then if I want to build another activity that finds these Proximity Alerts, how can I do that? Is that even possible?

Upvotes: -1

Views: 146

Answers (2)

Pablo Baxter
Pablo Baxter

Reputation: 2234

You have to maintain your own list of proximity alerts. There is no way to get them back. However, @Mercato is correct when he says that you can remove a PA using only pending intents, but you don't have to store them. According to the docs:

A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.

This means that the system will store your PendingIntent for you between app restarts, and you can retrieve it by passing the same Intent you used to create it. So for example, if you created the following PendingIntent:

Intent intent = new Intent(context, Foo.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Then all you have to store is the requestId (1) and the Class or class name (Foo.class or Foo.class.getName()). Then if you want to retrieve that same PendingIntent without creating a new one, you can do the following:

Class<Foo> className = retrieveClass(); //You implement this
//String clazz = retrieveClassName(); //This is another option
int requestId = retrieveId(); //You implement this

Intent intent = new Intent(context, className);

//The flag given attempts to retrieve the PendingIntent if it exists, returns null if it doesn't.
PendingIntent pi = PendingIntent.getBroadcast(context, requestId, intent, PendingIntent.FLAG_NO_CREATE);
if (pi != null) {
    //This pending intent was registered once before. 
    //Go ahead and call the function to remove the PA. Also, go ahead and call pi.cancel() on this.
}
else {
    //This pending intent was not registered, and therefore can't have a PA registered to it. 
} 

Upvotes: 1

Mercato
Mercato

Reputation: 569

Technically, all proximity alerts need a PendingIntent defined and used as a parameter. Android's Documentation shows that if you know the list of PendingIntents then you can remove them as well.

removeProximityAlert(PendingIntent intent) Removes the proximity alert with the given PendingIntent.

Since PendingIntent is Parecelable see here then you could add it as an Extra to any Intent. This means, that on starting another Activity, you can create an Parcelable[] array to hold all these PendingIntent, then

putExtra(String name, Parcelable[] value) Add extended data to the intent.

then retrieve them in the next Activity via getIntent() and it's relevant methods.

Upvotes: 1

Related Questions