3

我以这种方式创建接近警报

    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 时触发。欢迎任何帮助。

4

1 回答 1

6

您需要使用任何唯一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 时会触发广播。如果您进入与当前区域重叠的另一个区域 ZONE2,您不会收到警报,因为您仍在 ZONE1 中。您必须离开 ZONE1,因此在 enter=false 时广播将再次触发。因此,一旦您离开 ZONE1,如果您到达 ZONE2,它将触发广播 enter=true ZONE2。

我已经测试过了,它工作得很好。从市场上获取Location Spoofer免费应用程序并模拟手机的位置。您还需要在手机设置中启用模拟位置。并为您的应用程序添加额外的权限:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

我会做什么,将我的位置设置在离我很远的地方,可能是格陵兰岛,然后将位置设置在触发 ZONE1 的区域中,广播应该触发。然后再次将我的位置设置为格陵兰,并设置触发 ZONE2 的位置,广播应该触发。

进入标志可以从intent extras中获得

Bundle b = intent.getExtras();
Boolean entering = (Boolean) b.get(android.location.LocationManager.KEY_PROXIMITY_ENTERING);

我使用上面的代码为 100 个 POI 设置了接近警报,并且一切正常。

于 2010-07-21T11:17:36.740 回答