使用同一广播设置 2 个接近警报
我以这种方式创建接近警报
private void setProximityAlert(float radius, double lat, double lng, String place)
{
long expiration = -1;
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(TREASURE_PROXIMITY_ALERT);
intent.putExtra("lat", lat);
intent.putExtra("lng", lng);
intent.putExtra("place", place);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), -1, intent, 0);
locManager.addProximityAlert(lat, lng, radius, expiration, pendingIntent);
}
,并在我的活动中以这种方式注册接收器
IntentFilter intentFilter = new IntentFilter(TREASURE_PROXIMITY_ALERT);
registerReceiver(new ProximityIntentReceiver(), intentFilter);
setProximityAlert(10, 45.150344, 9.999815, "POINT1");
,并且我的广播接收器被正确调用。 现在,我想添加另一个接近警报,可以吗?我希望同一个广播接收器被 2 个邻近警报调用。 我做了这个:
IntentFilter intentFilter1 = new IntentFilter(TREASURE_PROXIMITY_ALERT1);
registerReceiver(new ProximityIntentReceiver(), intentFilter1);
setProximityAlert(200f, 45.143848, 10.039741, "POINT2");
但它不起作用,什么也没有发生。我现在真的很想知道这是否是正确的方法。我的目的是触发 2 个警报,一个是在 GPS 获取位置 POINT1 时触发,另一个是在位置 POINT2 时触发。 欢迎任何帮助。
I create a proximity alert in this way
private void setProximityAlert(float radius, double lat, double lng, String place)
{
long expiration = -1;
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Intent intent = new Intent(TREASURE_PROXIMITY_ALERT);
intent.putExtra("lat", lat);
intent.putExtra("lng", lng);
intent.putExtra("place", place);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), -1, intent, 0);
locManager.addProximityAlert(lat, lng, radius, expiration, pendingIntent);
}
and on my activity I registered the receiver in this way
IntentFilter intentFilter = new IntentFilter(TREASURE_PROXIMITY_ALERT);
registerReceiver(new ProximityIntentReceiver(), intentFilter);
setProximityAlert(10, 45.150344, 9.999815, "POINT1");
and my broadcast receiver is correctly called.
So now, I want to add another proximity alert, is it possible? I want that the same boadcast receiver is called by 2 proximity alert.
I made this:
IntentFilter intentFilter1 = new IntentFilter(TREASURE_PROXIMITY_ALERT1);
registerReceiver(new ProximityIntentReceiver(), intentFilter1);
setProximityAlert(200f, 45.143848, 10.039741, "POINT2");
but it does not work, nothing happen. I'm really now on it and I was wondering if it is the right way. My intent is trigger 2 alerts, one when GPS get the position POINT1 and another one at the position POINT2.
Any helps are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用任何唯一的
setAction
,以便系统认为这两个意图不同,否则将倾向于重用第一个意图。我有这个代码:
另请注意,接近警报的工作有点棘手。
用户根据您设置的信号精度和半径进入热点ZONE1。输入 = true ZONE1 时会触发广播。如果您进入与当前区域重叠的另一个区域 ZONE2,您不会收到警报,因为您仍在 ZONE1 中。
您必须离开 ZONE1,因此广播将在输入 = false 时再次触发。因此,一旦您离开 ZONE1,如果您到达 ZONE2,它将触发广播 Entering=true ZONE2。
我已经测试过了,效果很好。从市场上获取
Location Spoofer
免费应用程序并模拟手机的位置。您还需要在手机设置中启用模拟位置。并向您的应用程序添加额外的权限:我会做什么,将我的位置设置为远离我,可能是格陵兰岛,然后将位置设置在触发 ZONE1 的区域中,广播应该触发。然后再次将我的位置设置为格陵兰,并设置触发 ZONE2 的位置,广播应该触发。
输入标志可以从意图附加中获取,
我使用上面的代码为 100 个 POI 设置接近警报,并且一切正常。
You need to use whatever unique
setAction
so the system consider the two intents different, as otherwise will tend to reuse the first one.I have this code:
Also note that the proximity alert works kinda tricky.
User enters the hot ZONE1 based on the signal precision and radius you set. Broadcast is fired for entering=true ZONE1. If you enter another zone ZONE2 that overlap with the current zone you don't get the alert as you are still in ZONE1.
You must leave the ZONE1, so the broadcast will fire again with entering=false. So once now you left ZONE1, if you arrive ZONE2 it will fire the broadcast entering=true ZONE2.
I've tested and it works just fine. Grab
Location Spoofer
free application from market and mock the location of the phone. You also need to enable mock locations in the phones Setting. And add additional permission to your application:What I would do, set my location far away from me, probably Greenland, then set the position in a zone that triggers ZONE1, broadcast should fire. Then set again my location to Greeland, and set position that triggers ZONE2, broadcast should fire.
The entering flag can be get from the intent extras
I used the above codes to setup proximity alerts for 100 POIs and all work well.