Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addProximityAlert and KEY_PROXIMITY_ENTERING

Tags:

java

android

In the documentation, when discussing addProximityAlert , the description about Intent confuses me a bit. Specifically this part..

The fired Intent will have a boolean extra added with key KEY_PROXIMITY_ENTERING. If the value is true, the device is entering the proximity region; if false, it is exiting.

This may sound like a dumb question but.. how do I get true or false when I'm entering/ or in a certain radius of a location.

I'm not sure how this would work exactly.

Do I have to write my own code and check when I'm in the proximity of my location and then have it return true and false as I'm exiting?

I can't figure it out.

like image 640
Space Ghost Avatar asked Apr 27 '14 12:04

Space Ghost


2 Answers

I believe this works in conjunction with a BroadcastReceiver. You might set the addProximityAlert() method to fire the onReceive() method on this receiver, which will provide you an Intent as a parameter and then get that extra Boolean called KEY_PROXIMITY_ENTERING. So, step by step:

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

// You need to declare an Intent for your class and declare a PendingIntent on it,
// so it might be passed to the addProximityAlert method as the fifth parameter.
Intent intent = new Intent("com.yourdomain.yourproject.MyAlert");
PendingIntent proxIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

lm.addProximityAlert(your_latitude, your_longitude, RADIUS_IN_METERS, -1, proxIntent);

Explaination of the parameters set here:

  • your_longitude: The longitude you want to calculate the Radius on
  • your_latitude: The latitude you want to calculate the Radius on
  • RADIUS_IN_METERS: This is a constant defined by yourself where you specify the length of the radius you want to trace from the coordinate defined by the above parameters. If you set it to be, for example, 1000, you're saying that if someone gets closer than 1 km to your coordinate, the KEY_PROXIMITY_ENTERING will be true if the previous execution of onReceive() was more distant than 1 km, and analogously otherwise.
  • -1: This is the time in milliseconds after which the proximity alert will stop. If set to -1, it will never expire.
  • proxIntent: The proximity Intent that will fire your BroadcastReceiver.

Further, you'll also need this:

IntentFilter filter = new IntentFilter("com.yourdomain.yourproject.MyAlert"); 
registerReceiver(new AlertOnProximityReceiver(), filter);    

This way you're activating the Receiver for the filter you just set. This will fire the onReceive() event on it. So now, there's just one thing left, declare the BroadcastReceiver.

public class AlertOnProximityReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(final Context context, final Intent intent) {
    Boolean getting_closer = intent.getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, false);
    if (getting_closer)
      Log.d("Radius", "Hey, I just entered your radius!");
    else
      Log.d("Radius", "I just exited your radius!");
  }          
}

---- UPDATE ----

I'm now seeing this in the documentation:

Throws

SecurityException if ACCESS_FINE_LOCATION permission is not present

So make sure you include this permission in your AndroidManifest.xml file.

like image 164
nKn Avatar answered Nov 15 '22 17:11

nKn


Using addProximityAlert you specify the "special area" which should fire the proximity alert using the arguments it have (bold)

Parameters

latitude the latitude of the central point of the alert region

longitude the longitude of the central point of the alert region

radius the radius of the central point of the alert region, in meters expiration

time for this proximity alert, in milliseconds, or -1 to indicate no expiration

intent a PendingIntent that will be used to generate an Intent to fire when entry to or exit from the alert region is detected

When the user enters the area declared when you called the method the intent is called and inside this you found this extra KEY_PROXIMITY_ENTERING which alerts you if you enter or leave this area.

I don't know really how it works, but it could be something like:

To know it will check if he is in the radius latitude and longitudine, if yes send the intent when he leaves call the intent again passing "false".

The answer to your question is anyway you shouldn't care about this fact, it's done by the System and you don't need to do anything.

The only thing you need to do is to read this extra and use it if you need.

From documentation:

Due to the approximate nature of position estimation, if the device passes through the given area briefly, it is possible that no Intent will be fired. Similarly, an Intent could be fired if the device passes very close to the given area but does not actually enter it.

like image 29
Marco Acierno Avatar answered Nov 15 '22 17:11

Marco Acierno