How to receive location info when receiving a proximity alert intent?
I need to add more than one proximity alert with addProximityAlert method from LocationManager. The problem is that when i receive the intent informing that the user is near to one of the regions that i am listening for alerts i can get if the user is entering or exiting in a region rea开发者_StackOverflow中文版ding KEY_PROXIMITY_ENTERING, but i dont know what is the region he is entering or exiting. Is there any way the get this kind of info?
well, to add a ProximityAlert you need a PendingIntent, a PeindingIntent can be obtained with PendingIntent.getBroadcast(aContext, aRequestCode, anIntent, 0); Now, in the third parameter 'anIntent' you can put any data you want, like a Location object, a city name obtained with the location,
i do this like this:
private static final float DEFAULT_PROXIMITY = 1000f;
int requestCode = 0;
for (Location p : locationList) {
Double _lat = p.getLatitude();
Double _lon = p.getLongitude();
float radious = DEFAULT_PROXIMITY;
long expiration = -1;
Intent intent = new Intent(PROXIMITY_ALERT_ACTION_FIRED);
intent.putExtra( "location-lat" , p.getLatitude());
intent.putExtra( "location-lon" , p.getLongitude());
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, requestCode, intent, 0);
locationManager.addProximityAlert(_lat, _lon, radious, expiration, proximityIntent);
requestCode++;
}
now, when you receive the broadcast message, you must obtain the location info from the passed intent, something like this;
@Override
public void onReceive(Context context, Intent intent) {
Boolean entering = intent.getBooleanExtra(key, false);
if(entering){
long lat = intent.getLongExtra("location-lat", -1);
long lon = intent.getLongExtra("location-lon", -1);
log.info("", "entering: " + lat + lon);
}else{
long lat = intent.getLongExtra("location-lat", -1);
long lon = intent.getLongExtra("location-lon", -1);
log.info("", "exiting: " + lat + lon);
}
}
cheers
Define multiple Actions for every region you are interested in
精彩评论