addProximityAlert和KEY_PROXIMITY_ENTERINGaddProximityAlert、KEY_PROXIMITY_ENTERING

2023-09-07 08:39:19 作者:策失

在文档,讨论 addProximityAlert 时,关于意图的描述混淆了我一下。特别是这部分。

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

在发射意图将有一个额外的布尔与主要添加   KEY_PROXIMITY_ENTERING。如果该值为true,该设备进入   附近区域;如果为false,它正在退出。

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.

我必须写我自己的code和检查时,我在我的位置附近,然后使其返回true和false因为我退出?

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?

我不明白。

推荐答案

我相信这部作品在与的BroadcastReceiver 一起使用。你可以设置 addProximityAlert()的方法来开火该接收器的的onReceive()的方法,这将提供你一个意图作为参数,并再获得额外的布尔名为 KEY_PROXIMITY_ENTERING 。因此,分步实施:

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 :经度要计算半径上 your_latitude :纬度你要计算的半径上 RADIUS_IN_METERS :这是你自己,你指定你想从坐标通过上面的参数定义跟踪半径的长度定义的常量。如果你将其设置为,例如, 1000 ,你说如果有人越接近到1公里到你的协调,在 KEY_PROXIMITY_ENTERING 如果previous执行的onReceive的()比更遥远1公里,否则类似。 1 :这是时间(毫秒)之后,接近警报将停止。如果设置为 1 ,它永远不会过期。 proxIntent :接近意图,将解雇你的的BroadcastReceiver 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);    

这样你激活的接收器为你刚才设置的过滤器。这将触发的onReceive()事件就可以了。所以,现在,有一件事离开,宣告的BroadcastReceiver

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!");
  }          
}

----更新----

我现在看到了这个在documentation:

抛出

SecurityException若ACCESS_FINE_LOCATION权限不是present

SecurityException if ACCESS_FINE_LOCATION permission is not present

因此​​,请务必在您的的Andr​​oidManifest.xml 文件此权限。

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