Hi all,

I'm writing a scavenger hunt app and want to use the Proximity Alert functions. I'm having a hard time to finding any decent tutorials to help me with this. Does anyone have a code snippet on it's use that I can go off of?

Much appreciated,
Hendo

I think there's a LocationManager class in android that can help you.

There is. I just don't understand exactly how to implement it for proximity alerts. I'm trying to write a scavenger hunt app. I have the GPS points for the 10 locations. Once the device gets within the radius of one of the locations, I want some text to show up on the screen.

I am using the location manager to getLastKnowLocation, though I don't even think that is necessary. I want to use the ProximityAlert functions to pop up the appropriate text for the relevent locations. I just can't find any helpful tutorials for writing that code.

Thanks,
Hendo

I just can't find any helpful tutorials for writing that code.

Have you tried googling for "android proximity alert" and clicking the first result?

Traevel, I have done that one. And actually, I'm using that to gather my GPS points...it's kind of handy.

I am using LocationManager to add proximity way points.
lm.addProximityAlert (38.19776, -84.8672, 3, 5000, proximityIntent);

I just can't figure out how to get the text to show on the screen when I trigger that proximity alert. All the text is in my String.xml file. How do I get the text to pop up in my TextView box?

I feel like an idiot. I thought this was going to be pretty straight forward.

Thanks,
Hendo

Ok, forget about getting the text to show up, the proximity alerts are just not firing off. Here's the main code...

public class FCRun extends Activity {

    private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 9;                // in meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATE = 15000;                  // in Milliseconds
    private static final long POINT_RADIUS = 6;                                     // in meters
    private static final long PROX_ALERT_EXPIRATION = -1;                           
    private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
    private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";

    private static final String PROX_ALERT_INTENT = "King Hendo Tools";

    private LocationManager lm;
    private MyLocationListener mylistener;

    private IntentFilter intFilter;


     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fcrun);

            // Get the location Manager  (code from http://examples.javacodegeeks.com/android/core/location/android-location-based-services-example/)
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            lm.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATE,
                    MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                    new MyLocationListener());

            // the last known location of the provider
            Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

            mylistener = new MyLocationListener();

            if (location != null) {
                mylistener.onLocationChanged(location);
            } else {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
            // location updates: at least 15 meters and 10 seconds change
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 15, mylistener);

     }

    private class MyLocationListener implements LocationListener {
         @Override
         public void onLocationChanged(Location location) {

         }
         @Override
         public void onProviderDisabled(String provider) {

         }
         @Override
         public void onProviderEnabled(String provider) {

         }
         @Override
         public void onStatusChanged(String provider, int status, Bundle extras) {

         }
     }

     private void addProximityAlert(double latitude, double longitude) {
         Intent intent = new Intent(PROX_ALERT_INTENT);
         PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

         /*
         lm.addProximityAlert(
                 latitude, // the latitude of the central point of the alert region
                 longitude, // the longitude of the central point of the alert region
                 POINT_RADIUS, // the radius of the central point of the alert region, in meters
                 PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
                 proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
                 );
                 */  
         lm.addProximityAlert (38.19776, -84.8672, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.196, -84.8674, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.19518, -84.8666, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.19472, -84.8661, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.19388, -84.8649, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.19353, -84.8647, 6, 10000, proximityIntent);
         lm.addProximityAlert (38.19373, -84.8661, 6, 10000, proximityIntent);

         IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
         registerReceiver(new ProximityIntentReceiver(), filter);
     }

     @Override
     protected void onResume() {
         super.onResume();
         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 15, mylistener);
     }

     @Override
     protected void onPause() {
         super.onPause();
         lm.removeUpdates(mylistener);
     }



}

And here is the receiver code...

public class ProximityIntentReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1000;

    @Override
    public void onReceive(Context context, Intent intent) {
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = intent.getBooleanExtra(key, false);
        if (entering) {
            Log.d(getClass().getSimpleName(), "entering");

        }
        else {
            Log.d(getClass().getSimpleName(), "exiting");
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);


        Notification notification = createNotification();
        // notification.setLatestEventInfo(context, "Proximity Alert", "You are near your point of interest.", pendingIntent);
        notification.setLatestEventInfo(context, "Proximity Alert", "You are near your point of interest.", pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);

    }


    private Notification createNotification() {
        Notification notification = new Notification();

        notification.icon = R.drawable.ic_launcher;
        notification.when = System.currentTimeMillis();

        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;

        return notification;
    }

}

What am I missing here? When I send GPS coordinates throught the DDMS, my alerts are not firing off.

Thanks,
Hendop

Ok, I've resolved the proximity alert problem. I am now firing off the various alerts that I've added.

The last part of this problem is that I have assigned an ID for each of the proximity alerts. I need to pass that ID to the receiver (I think) so that when a proximity alert is fired, the corresponding ID can be used to populate the text fields. Here's the updated code for the activity:

public class FCRun extends Activity implements LocationListener  {

    private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1;                // in meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATE = 5000;                   // in Milliseconds
    private static final long EXPIRE = -1;                          // -1 is never expires
    public static final String PROX_ALERT_INTENT = "com.kinghendo.android.frankcem.ProximityAlert";
    //public static final int KEY_LOCATION_CHANGED = 0;
    private LocationManager lm;
    double latitude, longitude;
    public int ID;
    public String[] Screen;
    private ProximityIntentReceiver proximityReceiver;
    //private String[] locationList;

    // setting default screen text
    public TextView txtName;
    public TextView txtInfo;
    public TextView txtClue;



     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fcrun);

            @SuppressWarnings("unused")
            Resources res = getResources();

            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.first);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+PROX_ALERT_INTENT);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.first);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+PROX_ALERT_INTENT);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.first);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+PROX_ALERT_INTENT);


         // Get the location Manager  (code from http://examples.javacodegeeks.com/android/core/location/android-location-based-services-example/)
            lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            lm.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    MINIMUM_TIME_BETWEEN_UPDATE,
                    MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                    this
            );

            IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
            proximityReceiver = new ProximityIntentReceiver();                      // registers ProximityIntentReceiver
            registerReceiver(proximityReceiver, filter);

            addProximityAlerts();

     }

     private void addProximityAlerts(){
         Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
         if (loc == null)
             Toast.makeText(this, "No location", Toast.LENGTH_SHORT).show();
         else
             addProximityAlert(loc.getLatitude(), loc.getLongitude(), 3, 0);
             addProximityAlert (38.042015, -84.492637, 10, 27);                 // test Awesome
             addProximityAlert (38.085705, -84.56110000000001, 10, 26);         // Test Home Location
             addProximityAlert (38.15264962, -84.89520585, 10, 25);             // Test Office Location
             addProximityAlert (38.19787138, -84.86692458, 10, 1);              // Information Center
             addProximityAlert (38.19600161, -84.86743554, 6, 2);               // Goebel
             addProximityAlert (38.2031917, -84.8676742, 7, 3);                 // Chapel
             addProximityAlert (38.1921737, -84.870451, 6, 4);                  // Confederate Cemetery
             addProximityAlert (38.1934558, -84.8685347, 2, 5);                 // O'Bannon
             addProximityAlert (38.19381509, -84.86490403, 2, 6);               // Henry Clay Jr
             addProximityAlert (38.0873887, -84.5475033, 2, 7);                 // O'Hara
             addProximityAlert (38.1916428, -84.8709677, 10, 8);                // Daniel Boone
     }


    private void addProximityAlert(double latitude, double longitude, int radius, int ID) {
        Log.i("TEST", "addProximityAlert "+latitude+", "+longitude+", "+radius+", "+ID+", "+EXPIRE);

        Intent intent = new Intent(PROX_ALERT_INTENT);
        intent.putExtra("ID", ID);
        PendingIntent proximityIntent = PendingIntent.getBroadcast(this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);    //FLAG_UPDATE_CURRENT); instead of 0);

        lm.addProximityAlert(latitude, longitude, radius, ID, proximityIntent);
        //lm.addProximityAlert(latitude, longitude, radius, EXPIRE, proximityIntent);

    }

     @Override
     public void onLocationChanged(Location location) {
         int ID = getIntent().getIntExtra("ID", 0);

         //String provider = location.getProvider();
         latitude = location.getLatitude();
         longitude = location.getLongitude();
         //int ID = Integer.valueOf(PROX_ALERT_INTENT);
         Log.i("TEST", "lat: "+latitude+" lng: "+longitude+" "+PROX_ALERT_INTENT);

         switch (ID){
         case 1:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.start);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.start);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.start);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 2:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.goebel);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.goebel);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.goebel);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 3:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.church);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.church);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.church);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 4:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.confederate);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.confederate);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.confederate);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 5:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.obannon);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.obannon);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.obannon);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 6:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.hcj);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.hcj);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.hcj);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 7:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.ohara);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.ohara);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.ohara);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 8:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.danielboone);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.danielboone);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.danielboone);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 25:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.toffice);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.toffice);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.toffice);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 26:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.thome);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.thome);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.thome);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         case 27:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.tawesome);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.tawesome);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.tawesome);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
             break;
         default:
            txtName = (TextView) findViewById(R.id.txtName);
            Screen = getResources().getStringArray(R.array.first);
            txtName.setText(Screen[0]);
            Log.i("txtName", "populated "+ID);

            txtInfo = (TextView) findViewById(R.id.txtInfo);
            Screen = getResources().getStringArray(R.array.first);
            txtInfo.setText(Screen[1]);
            Log.i("txtInfo", "populated "+ID);

            txtClue = (TextView)findViewById(R.id.txtClue);
            Screen = getResources().getStringArray(R.array.first);
            txtClue.setText(Screen[2]);
            Log.i("txtClue", "populated "+ID);
            break;


         }


     }

     @Override
     public void onProviderDisabled(String provider) {

     }
     @Override
     public void onProviderEnabled(String provider) {

     }
     @Override
     public void onStatusChanged(String provider, int status, Bundle extras) {

     }

     @Override
     protected void onResume() {
         super.onResume();
     }

     @Override
     protected void onPause() {
         super.onPause();
     }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        lm.removeUpdates(this);
        unregisterReceiver(proximityReceiver);

    }
}

and here is the code for the receiver:

public class ProximityIntentReceiver extends BroadcastReceiver {

    private static final int NOTIFICATION_ID = 1000;
    private int ID = 0;
    //int i;

    @Override
    public void onReceive(Context context, Intent intent) {
        //int i;
        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Bundle results = getResultExtras(true);
        Boolean entering = intent.getBooleanExtra(key, false);

        if (entering) {
            long lat = intent.getLongExtra("location-lat", -1);
            long lon = intent.getLongExtra("location-lon", -1);
            int ID = intent.getIntExtra("ID", 0);
            Log.i("", "entering: " + lat + lon + ID);
            Log.d(getClass().getSimpleName(), "entering receiverrrrrrrrrrrrrrrrrr");
        }
        else {
            long lat = intent.getLongExtra("location-lat", -1);
            long lon = intent.getLongExtra("location-lon", -1);
            Log.i("", "exiting: " + lat + lon);
            Log.d(getClass().getSimpleName(), "exiting");
        }

     // Vibrate for 2 seconds
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(2000);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, ID, new Intent(FCRun.PROX_ALERT_INTENT), 0);        

        Notification noti = new Notification.Builder(context)
        .setContentTitle("Location Alert ")
        .setContentText("Entering Point of Interest")
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .build(); // available from API level 11 and onwards

        notificationManager.notify(NOTIFICATION_ID, noti);
     }
}

Obviously, I'm not getting my intents correct. Please help!

Thanks,
Hendo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.