我以这种方式创建接近警报
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);
}
在我的 Activity 中,我以这种方式注册了接收器
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");
但是它不起作用,没有任何反应。我现在真的在做,我想知道这是否是正确的方法。我的 Intent 是触发 2 个警报,一个是当 GPS 获得位置 POINT1 时,另一个是在位置 POINT2 时。 欢迎任何帮助。
最佳答案
您需要使用任何唯一的setAction
,以便系统认为这两个 Intent 不同,否则将倾向于重用第一个。
我有这个代码:
Intent intent = new Intent(this,PlacesProximityHandlerService.class);
intent.setAction("foo"+objPlace.getId());
intent.putExtra(Poi._ID, objPlace.getId());
intent.putExtra(Poi.LAT, objPlace.getLat());
intent.putExtra(Poi.LON, objPlace.getLon());
PendingIntent sender = PendingIntent.getService(this,0, intent, 0);
LocationUtils.addProximity(this, objPlace.getLat(),objPlace.getLon(), objPlace.getError(), -1,sender);
另请注意,接近警报的工作有点棘手。
用户根据您设置的信号精度和半径进入热点ZONE1。进入=true ZONE1 时触发广播。如果您进入与当前区域重叠的另一个区域 ZONE2,您将不会收到警报,因为您仍在 ZONE1 中。 您必须离开 ZONE1,因此广播将再次触发并输入=false。所以一旦你离开 ZONE1,如果你到达 ZONE2,它会触发广播 entering=true ZONE2。
我已经测试过了,它工作得很好。从市场上获取 Location Spoofer
免费应用程序并模拟手机的位置。您还需要在手机设置中启用模拟位置。并为您的应用程序添加额外的权限:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
我会做的,将我的位置设置在离我很远的地方,可能是格陵兰岛,然后将位置设置在触发 ZONE1 的区域中,广播应该会触发。然后再次将我的位置设置为Greeland,并设置触发ZONE2的位置,广播应该触发。
进入标志可以从intent extras中获取
Bundle b = intent.getExtras();
Boolean entering = (Boolean) b.get(android.location.LocationManager.KEY_PROXIMITY_ENTERING);
我使用上面的代码为 100 个 POI 设置了接近警报并且一切正常。
关于android - 使用相同的广播设置 2 个接近警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3298388/