AddProximityAlert and KEY_PROXIMITY_ENTERING

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

An additional value will be added to the shot intention with the addition of the key KEY_PROXIMITY_ENTERING. If true, the device enters an area of โ€‹โ€‹proximity; if false, it terminates.

This may seem like a silly question, but ... how can I get the truth or false when I enter / or within a certain radius of the location.

I'm not sure how this will work.

Should I write my own code and check when I'm near my location, and then return it true and false when I exit?

I can not understand.

+7
java android
source share
3 answers

I believe this works in conjunction with BroadcastReceiver . You can set the addProximityAlert() method to run the onReceive() method on this receiver, which will provide you with Intent as a parameter, and then get an 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); 

Explanation of the parameters set here:

  • your_longitude : the longitude you want to calculate the radius
  • your_latitude : the latitude you want to calculate the radius
  • RADIUS_IN_METERS . This is a constant that you define, where you specify the length of the radius you want to track from the coordinate defined by the above parameters. If you set it to 1000 , for example, you say that if someone KEY_PROXIMITY_ENTERING closer to your coordinate closer than 1 km, KEY_PROXIMITY_ENTERING will be true if the previous execution of onReceive() was more distant than 1 km, and similarly otherwise .
  • -1 . This is the time in milliseconds after which the proximity warning will stop. If set to -1 , it will never expire.
  • proxIntent : The Intent affinity that will launch your BroadcastReceiver .

In addition, you will also need the following:

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

This way you activate the receiver for the selected filter. This will fire the onReceive() event. So now there is only one left, declare 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 ----

Now I see this in the documentation:

Gives out

SecurityException if ACCESS_FINE_LOCATION permission is missing

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

+6
source share

Using addProximityAlert , you specify a "special area" that should trigger a proximity alert using the arguments it has ( bold )

Options

latitude latitude of the central alert point region

longitude longitude of the central alert point region

radius radius of the center point of the warning zone, in meter expiration date

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

The PendingIntent intent that will be used to generate the intent to start when entering or exiting an alert is detected.

When the user enters the declared scope, when you call the method called intent , and inside it you find this extra KEY_PROXIMITY_ENTERING , which warns you if you enter or leave this scope.

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

To know that he will check whether he is in the radius of latitude and longitudinality, if so, send the intention when he leaves the call, the intention is again transmitted โ€œfalseโ€.

The answer to your question, in any case, you should not care about this fact, it is done by the system, and you do not need to do anything.

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

From the documentation:

Due to the approximate nature of the position assessment, if the device passes through a given area in brief, it is possible that the intention will not be triggered. Similarly, Intent can be triggered if the device passes very close to a given area, but does not actually enter it.

+1
source share

Use this code, it will help you a lot.

 Intent intent = new Intent(PROX_ALERT_INTENT); PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0); locationManager.addProximityAlert( lat, // the latitude of the central point of the alert region lon, // the longitude of the central point of the alert region POINT_RADIUS, // the radius of the central point of the alert region, in meters PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected ); IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); registerReceiver(new ProximityIntentReceiver(orderStatusObject.getBidId()), filter); 

Class - for approximate broadcast notification

 public class ProximityIntentReceiver extends BroadcastReceiver{ private static final int NOTIFICATION_ID = 1000; public Context mcontext;//Context of calling BroadCast Receiver private String bidId; //Hold the value of bid Id public ProximityIntentReceiver() { // TODO Auto-generated constructor stub } public ProximityIntentReceiver(String bidId) { // TODO Auto-generated constructor stub this.bidId = bidId; } @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub 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"); } sendNotification(context); } public void sendNotification(Context mcontext){ // String extra=arg1.getExtras().getString("alert").toString(); long when = System.currentTimeMillis(); String message = "You are near of driver pickup area."; NotificationManager notificationManager = (NotificationManager) mcontext.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher,message, when); String title = "Proximity Alert!"; Intent notificationIntent = new Intent(); // set intent so it does not start a new activity notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(mcontext, 0,notificationIntent, 0); notification.setLatestEventInfo(mcontext, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults = Notification.DEFAULT_ALL; notificationManager.notify(0, notification); } 
0
source share

All Articles