Java addProximityAlert和按键\u接近\u输入
在文档中,当讨论Java addProximityAlert和按键\u接近\u输入,java,android,Java,Android,在文档中,当讨论addProximityAlert时,关于意图的描述让我有点困惑。特别是这一部分 激发的意图将添加一个带有键的布尔额外值 按键\接近\进入。如果该值为真,则设备正在输入 邻近区域;如果为false,则它正在退出 这听起来可能是个愚蠢的问题,但。。当我进入/或在某个位置的某个半径内时,如何获得真或假 我不确定这到底是怎么回事 我是否必须编写自己的代码,并在靠近我的位置时进行检查,然后在退出时返回true和false 我想不出来。使用您指定的“特殊区域”,该区域应使用其具有的参数(b
addProximityAlert
时,关于意图的描述让我有点困惑。特别是这一部分
激发的意图将添加一个带有键的布尔额外值
按键\接近\进入。如果该值为真,则设备正在输入
邻近区域;如果为false,则它正在退出
这听起来可能是个愚蠢的问题,但。。当我进入/或在某个位置的某个半径内时,如何获得真或假
我不确定这到底是怎么回事
我是否必须编写自己的代码,并在靠近我的位置时进行检查,然后在退出时返回true和false
我想不出来。使用您指定的“特殊区域”,该区域应使用其具有的参数(bold)触发接近警报
参数
纬度警报中心点的纬度
区域
经度警报中心点的经度
区域
半径警报区域中心点的半径,以
仪表失效
此接近警报的时间,以毫秒为单位,或
-1表示没有到期
意图当进入或退出警报时,用于生成开火意图的悬挂式帐篷
区域被检测到
当用户进入您调用该方法时声明的区域时,会调用意图
,在该区域内,您会发现此额外的按键_接近_进入
,当您进入或离开此区域时会向您发出警告
我真的不知道它是如何工作的,但可能是这样的:
要知道它将检查他是否在半径纬度和经度,如果是,在他离开时发送意图,再次通过“假”调用意图
你的问题的答案是,无论如何,你不应该关心这个事实,它是由系统完成的,你不需要做任何事情
你需要做的唯一一件事就是多读这篇文章,如果你需要的话就使用它
从文件:
由于位置估计的近似性质,如果设备短暂通过给定区域,则可能不会触发任何意图。类似地,如果设备经过非常靠近给定区域但实际上没有进入该区域,则可能会触发意图
我相信这与
广播接收器
配合使用会起作用。您可以设置addProximityAlert()
方法在此接收器上触发onReceive()
方法,该方法将为您提供Intent
作为参数,然后获取名为KEY\u approxity>的额外布尔值。因此,一步一步:
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
,您的意思是,如果某人离您的坐标距离超过1km,则如果先前执行的onReceive()
距离超过1km,则输入的键将为true
- -1:这是接近警报停止的时间(毫秒)。如果设置为
-1
,它将永远不会过期
- proxIntent:将触发您的
广播接收器的接近意图
此外,您还需要:
IntentFilter filter = new IntentFilter("com.yourdomain.yourproject.MyAlert");
registerReceiver(new AlertOnProximityReceiver(), filter);
这样,您就激活了刚刚设置的过滤器的接收器。这将对其触发onReceive()
事件。现在,只剩下一件事了,声明广播接收器
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!");
}
}
----更新---
我现在看到的是:
投掷
如果不存在访问\u精细\u位置权限,则出现安全异常
因此,请确保在您的AndroidManifest.xml
文件中包含此权限。使用此代码将对您有很大帮助
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);
}