在文档中,当讨论addProximityAlert时,关于意图的描述让我有点困惑。特别是这部分。
激发的意图将有一个布尔额外添加键KEY_PROXIMITY_ENTERING。如果值为真,则设备进入邻近区域;如果为false,则退出。
这听起来可能是个愚蠢的问题,但是..。当我进入/或在某一位置的某一半径时,如何才能得到真或假。
我不知道这到底怎么回事。
我是否必须编写自己的代码,并在接近我的位置时检查,然后在退出时返回true和false?
我搞不懂。
发布于 2014-04-29 20:00:11
我相信这与BroadcastReceiver一起工作。您可以将addProximityAlert()方法设置为在此接收器上触发onReceive()方法,该方法将为您提供一个Intent作为参数,然后获得名为KEY_PROXIMITY_ENTERING的额外Boolean。因此,一步一步:
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);对此处设置的参数的解释:
1000,您的意思是,如果有人离您的坐标超过1公里,那么如果以前onReceive()的执行距离超过1公里,则KEY_PROXIMITY_ENTERING将是true,而类似的情况则不然。-1,它将永远不会过期。BroadcastReceiver的邻近Intent。此外,您还需要这样做:
IntentFilter filter = new IntentFilter("com.yourdomain.yourproject.MyAlert");
registerReceiver(new AlertOnProximityReceiver(), filter); 这样你就可以启动你刚设置的过滤器的接收器了。这将触发onReceive()事件。所以,现在只剩下一件事,声明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!");
}
}-更新-
我现在在文档上看到了这个
抛出 如果不存在SecurityException权限,则为ACCESS_FINE_LOCATION
因此,请确保在AndroidManifest.xml文件中包含此权限。
发布于 2014-04-29 19:15:13
使用addProximityAlert,您可以指定“特殊区域”,使用它所具有的参数来触发邻近警报(粗体)
参数 纬度警报区域中心点的纬度 经度警报区域中心点的经度 radius 警报区域中心点的半径,以米为单位 此接近警报的时间,以毫秒为单位,或-1表示没有过期。 意图:当检测到进入或退出警报区域时,将用于生成发射意图的PendingIntent。
当用户进入当您调用该方法时声明的区域时,调用intent,并在其中找到一个额外的KEY_PROXIMITY_ENTERING,如果您输入或离开该区域,它会提醒您。
我不知道它是怎么工作的,但可能是这样的:
要知道这将检查他是否在半径纬度和经线,如果是的,发送意图时,他离开时,呼吁意图再次通过“假”。
对你的问题的答案是,无论如何,你不应该关心这个事实,它是由系统完成的,你不需要做任何事情。
你唯一需要做的就是读这个额外的,并使用它,如果你需要。
来自文件:
由于位置估计的近似性质,如果该设备短暂地通过给定的区域,则可能不会发射任何意图。同样,如果设备经过非常接近给定区域但实际上没有进入该区域,则可能会触发意图。
发布于 2014-05-02 06:14:10
使用这个代码会对你有很大的帮助。
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);类-用于近似警报广播
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);
}https://stackoverflow.com/questions/23323162
复制相似问题