Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

android - set 2 proximity alerts with the same broadcast

I create a proximity alert in this way

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

and on my activity I registered the receiver in this way

    IntentFilter intentFilter = new IntentFilter(TREASURE_PROXIMITY_ALERT);
    registerReceiver(new ProximityIntentReceiver(), intentFilter);
    setProximityAlert(10, 45.150344, 9.999815, "POINT1");

and my broadcast receiver is correctly called. So now, I want to add another proximity alert, is it possible? I want that the same boadcast receiver is called by 2 proximity alert. I made this:

    IntentFilter intentFilter1 = new IntentFilter(TREASURE_PROXIMITY_ALERT1);
    registerReceiver(new ProximityIntentReceiver(), intentFilter1);        
    setProximityAlert(200f, 45.143848, 10.039741, "POINT2");

but it does not work, nothing happen. I'm really now on it and I was wondering if it is the right way. My intent is trigger 2 alerts, one when GPS get the position POINT1 and another one at the position POINT2. Any helps are welcome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...