我已经阅读了几个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();
在对话框的 ClickListener 中,我称之为 SetProximityAlert(点):
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,则没有必要这样做。
提前致谢
经过多次尝试,我无法触发接近警报。我已经选择了替代解决方案(我想不是最佳的)
每次我当前位置变化时,我都会计算距离。我一次只有一个邻近警报,但我想如果我有很多,这不是最好的解决方案。
如果你还在找,这对我来说就像一个魅力:http://www.gauntface.co.uk/pages/blog/2010/01/04/proximity-alerts-in-android/