我已经阅读了几个theads,博客和论坛,但我找不到解决方案
我想要一个接近警报:
public class ProximityIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String key= LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering=intent.getBooleanExtra(key, false);
Log.i(QUEST_PROXIMITY_ALERT,"entering");
Log.i(QUEST_PROXIMITY_ALERT,"exiting");
}
}
在onCreateMethod中:
locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
provider=locationmanager.getBestProvider(criteria, true);
location=locationmanager.getLastKnownLocation(provider);
if (location!=null) updateWithNewLocation(location);
locationmanager.requestLocationUpdates(provider, 0, 0, locationlistener);
我的位置听众是:
private final LocationListener locationlistener= new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LOCATION_UPDATED","");
updateWithNewLocation(location);
mapview.invalidate();
}
public void onProviderDisabled(String provider) {
//Message TO-DO
}
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int sttus, Bundle extras) { }
};
在活动开始时,我向用户显示一个选择一些数据的对话框:
//Dialog to choose available EduQuests
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Selecciona una búsqueda");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
dialog.dismiss();
mCurrent=item;
Double lat=Double.parseDouble(mQuests.getItem(item).getPlace(0).getLatitude());
Double longi=Double.parseDouble(mQuests.getItem(item).getPlace(0).getLongitude());
GeoPoint point=new GeoPoint((int)(longi*1E6),(int) (lat*1E6));
SetProximityAlert(point);
mCurrentPlace=0; //First GeoPoint in the quest
mQuests.getItem(mCurrent).getPlace(0).setVisibility(true);
DrawPoints();
mapview.invalidate();
}
});
AlertDialog alert = builder.create();
alert.show();
当我调用SetProximityAlert(point)时,在Dialog的ClickListener中:
private void SetProximityAlert(GeoPoint point) {
double lat=point.getLatitudeE6();
double lng=point.getLongitudeE6();
Log.i("SETTING PROXIMITY ALERT LATTITUDE--->",Double.toString(lat));
Log.i("SETTING PROXIMITY ALERT LONGITUDE--->",Double.toString(lng));
float radio= 20; //In meters
long expiration=-1;
//Creo el Intent con la acción que hemos definido"
Intent intent= new Intent(QUEST_PROXIMITY_ALERT);
//Creo el PendingIntent pasándole
PendingIntent proximityIntent=PendingIntent.getBroadcast(this,0, intent, 0);
locationmanager.addProximityAlert(lat, lng, radio, expiration, proximityIntent);
//Promixity Alert
IntentFilter filter= new IntentFilter(QUEST_PROXIMITY_ALERT);
registerReceiver(new ProximityIntentReceiver(),filter);
}
我计算距离来检查是否应该在UpdateWithNewLocation()方法中触发警报:
private void updateWithNewLocation(Location location) {
Double lat=Double.parseDouble(mQuests.getItem(mCurrent).getPlace(mCurrentPlace).getLatitude());
Double longi=Double.parseDouble(mQuests.getItem(mCurrent).getPlace(mCurrentPlace).getLongitude());
Location distlocation=new Location(provider);
distlocation.setLatitude(lat);
distlocation.setLongitude(longi);
Log.i("DISTANCE TO CURRENT POINT--->",Float.toString(location.distanceTo(distlocation))+"...metros");
curlat=location.getLatitude();
curlong=location.getLongitude();
GeoPoint point=new GeoPoint((int)(curlat*1E6),(int) (curlong*1E6));
OverlayItem overlayitem=new OverlayItem(point,"I'm here","This is my current position");
if (curpositemizedoverlay==null) {
curpositemizedoverlay=new QuestItemizedOverlay(this.getResources().getDrawable(R.drawable.me));
}
else {
curpositemizedoverlay.removeItem(curpositemizedoverlay.size()-1);
}
curpositemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(curpositemizedoverlay);
mapview.invalidate();
}
private static String QUEST_PROXIMITY_ALERT="com.pekechis.ieda.proximityalert";
包名为com.pekechis.ieda,主要活动为IEDAQUEST。
我可以看到当前位置发生变化,因为我更改了DDMS中的设备位置。同时更改我在日志中显示的de POI(兴趣点)的距离。
但警报不会触发。
知道我做错了什么。
我认为这是在清单中声明过滤器的问题,但正如我已经读过的那样,如果我正在调用registereceiver,则不需要这样做。
提前致谢
答案 0 :(得分:0)
我每次当前位置变化时计算距离。我一次只有一个接近,但我想如果我有很多,这不是最好的解决方案。
答案 1 :(得分:0)
如果你还在寻找,这对我来说就像一个魅力:http://www.gauntface.co.uk/pages/blog/2010/01/04/proximity-alerts-in-android/