set 2 proximity alerts with the same broadcast

前端 未结 1 1415
借酒劲吻你
借酒劲吻你 2020-12-17 05:41

I create a proximity alert in this way

    private void setProximityAlert(float radius, double lat, double lng, String place)
{
    long expiration = -1;
            


        
1条回答
  •  悲&欢浪女
    2020-12-17 05:57

    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:

    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);
    

    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

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

    I used the above codes to setup proximity alerts for 100 POIs and all work well.

    0 讨论(0)
提交回复
热议问题