我用这种方法创建接近警报
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");
我的广播接收器被正确调用了。
所以现在,我想添加另一个接近警报,有可能吗?我要同一个船速接收器被两个接近警报调用。
我做了这个:
IntentFilter intentFilter1 = new IntentFilter(TREASURE_PROXIMITY_ALERT1);
registerReceiver(new ProximityIntentReceiver(), intentFilter1);
setProximityAlert(200f, 45.143848, 10.039741, "POINT2");
但它不起作用,什么也没发生。我现在真的很想知道这是不是正确的方法。我的目的是触发2个警报,一个在GPS得到位置点1,另一个在位置点2。
欢迎任何帮助。
最佳答案
您需要使用任何唯一的setAction
以便系统考虑两个不同的意图,否则将倾向于重用第一个意图。
我有这个密码:
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);
还需要注意的是,接近警报的工作有点棘手。
用户根据您设置的信号精度和半径输入热区1。如果输入=true zone1,则会触发广播。如果您输入与当前区域重叠的另一个区域2,则不会收到警报,因为您仍在区域1中。
您必须离开区域1,这样广播将在entering=false时再次启动。因此,一旦您离开Zone1,如果您到达Zone2,它将触发广播输入=true zone2。
我已经测试过了,效果还不错。从市场上获取免费应用程序并模拟手机的位置。您还需要在phones设置中启用模拟位置。并向应用程序添加其他权限:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
我要做的是,将我的位置设置在远离我的地方,可能是格陵兰岛,然后将位置设置在触发区域1的区域,广播应该会开火。然后再次将我的位置设置为greeland,并设置触发zone2的位置,广播将触发。
输入标志可以从intent extras获得
Bundle b = intent.getExtras();
Boolean entering = (Boolean) b.get(android.location.LocationManager.KEY_PROXIMITY_ENTERING);
我用上面的代码为100个poi设置了接近警报,一切正常。