使用相同的广播设置2个接近警报

bl4*_*4d3 3 alert gps android broadcastreceiver

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

    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);
}
Run Code Online (Sandbox Code Playgroud)

在我的活动中,我以这种方式注册了接收器

    IntentFilter intentFilter = new IntentFilter(TREASURE_PROXIMITY_ALERT);
    registerReceiver(new ProximityIntentReceiver(), intentFilter);
    setProximityAlert(10, 45.150344, 9.999815, "POINT1");
Run Code Online (Sandbox Code Playgroud)

我的广播接收器被正确调用.所以现在,我想添加另一个接近警报,是否可能?我希望2接近警报调用相同的boadcast接收器.我做的:

    IntentFilter intentFilter1 = new IntentFilter(TREASURE_PROXIMITY_ALERT1);
    registerReceiver(new ProximityIntentReceiver(), intentFilter1);        
    setProximityAlert(200f, 45.143848, 10.039741, "POINT2");
Run Code Online (Sandbox Code Playgroud)

但它不起作用,没有任何反应.我现在真的很喜欢它,我想知道它是否正确.我的意图是触发2个警报,一个是GPS获取位置POINT1而另一个是POINT2位置.欢迎任何帮助.

Pen*_*m10 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);
Run Code Online (Sandbox Code Playgroud)

另请注意,接近警报的工作有点棘手.

用户根据您设置的信号精度和半径输入热ZONE1.输入= true ZONE1会触发广播.如果您输入与当前区域重叠的另一个区域ZONE2,则当您仍在ZONE1中时,您不会收到警报.您必须离开ZONE1,因此广播将再次触发,输入= false.所以一旦你离开ZONE1,如果你到达ZONE2,它将发射广播进入=真ZONE2.

我已经测试过,它运行得很好.抓斗Location Spoofer从市场上免费的应用程序和模拟手机的位置.您还需要在手机设置中启用模拟位置.并为您的应用程序添加其他权限:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
Run Code Online (Sandbox Code Playgroud)

我会做什么,把我的位置设置在远离我的位置,可能是格陵兰岛,然后在触发ZONE1的区域设置位置,广播应该开火.然后再将我的位置设置为Greeland,并设置触发ZONE2的位置,广播应该触发.

输入标志可以从意图附加中获得

Bundle b = intent.getExtras();
Boolean entering = (Boolean) b.get(android.location.LocationManager.KEY_PROXIMITY_ENTERING);
Run Code Online (Sandbox Code Playgroud)

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