Question

I have read several theads, blogs and forum but I didn't find the solution

I want a Proximity Alert so:

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

        }       
    }  

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

And my location listener is:

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

At the beggining of the activity I show to the user a dialog to choose some data:

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

And in the ClickListener of the Dialog as you I call the SetProximityAlert(point):

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

I calculate the distance to check if the alert should be fired in UpdateWithNewLocation() method that works fine:

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

And private static String QUEST_PROXIMITY_ALERT="com.pekechis.ieda.proximityalert";

The package is name com.pekechis.ieda and the main activity IEDAQUEST.

I can see as my current position change as i change and the device position in DDMS. Also changes the distance to de POI (Point of Interest) that i shown in the log.

But the alert does not fire.

Any idea of what i'm doing wrong.

I thought It was a problem of declaring the filter in the manifest but as i have read there's no need for that if I'm calling registereceiver.

Thanks in advance

Was it helpful?

Solution

well after many tries i was impossible for me to fire the Proximity Alert. I have chosen and alternative solutions (not optimal I suppose)

I calculate the distance each time my current position changes. I have only one proximityalert at a time but I suppose that is no the best solution if I have many.

OTHER TIPS

In case you are still looking, this worked like a charm for me: http://www.gauntface.co.uk/pages/blog/2010/01/04/proximity-alerts-in-android/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top