Test Script

Saturday, October 6, 2012

Proximity Alerts

Ok here is a simple Tutorial on how to implement the Proximity Alerts in Android.This tutorial is a just a guide on how to use the Proximity Alerts feature in Android & in no way is a complete application by itself.

Here i am going to be using two classes to implement this:
1. MainActivity.java
2. ProximityReciever.java

The MainActvity is the class used to set the Proximity alerts. Here is my Implementation for MainActivity.java:

    public void import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
LocationManager lm;
double lat=123,long1=34;    //Defining Latitude & Longitude
float radius=3000;                         //Defining Radius
    public void onCreate(Bundle savedInstanceState) {
        lm=(LocationManager) getSystemService(LOCATION_SERVICE);
        Intent i= new Intent("com.adnan.proximityalert");           //Custom Action
        PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
        lm.addProximityAlert(lat, long1, radius, -1, pi);


Nothing special or any Rocket Science above , here is a small explantion of what i did:

1.Defined the latitude & longitude co-ordinates of the location for which we want to setup the alert.

2.Radius is range for which the alert should work.(This is in meters, so in my example 3km radius)

3.Create a new Intent with a custom action(in my example its "com.adnan.proximityalert")

4.Finally called the addProximityAlert method which takes in the latitude,longitude,radius,expiration time,Pending Intent)

Expiration time is in milliseconds & by setting it to -1 like i did tells the system that the alert never expires.

Now we need a Reciever to listen to the broadcast & do the further things


import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.widget.Toast;

/*This is the Reciever for the Brodcast sent, here our app will be notified if the User is 
* in the region specified by our proximity alert.You will have to register the reciever 
* with the same Intent you broadcasted in the previous Java file
 * @Author: Adnan A M 

public class ProximityReceiver extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
// The reciever gets the Context & the Intent that fired the broadcast as arg0 & agr1 
String k=LocationManager.KEY_PROXIMITY_ENTERING;
// Key for determining whether user is leaving or entering 
boolean state=arg1.getBooleanExtra(k, false);
//Gives whether the user is entering or leaving in boolean form
// Call the Notification Service or anything else that you would like to do here
Toast.makeText(arg0, "Welcome to my Area", 600).show();
//Other custom Notification 
Toast.makeText(arg0, "Thank you for visiting my Area,come back again !!", 600).show();



What i did above:

1.Created a Reciever which listens for the broadcast
2.Proximity Alerts use both GPS & Network Provider.
3.Alerts are fired when the user is entering the defined area or leaving the area, so it becomes necessary to determine whether the user is entering the zone or leaving , it is done using KEY_PROXIMITY_ENTERING, this gives a key which is used to retrieve the info whether the user is entering or leaving
4. As shown i used a boolean variable state to store whether the user is entering or Leaving & if no value is recieved i set the default to false
5.Based on the value of state i have two different Toasts.

You need to add the following permissions in manifest as well :

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Thats it ! You are all set to recieve a alert when you go in a 3kn range of the desired co ordinates !

I will consider writing a second version of this Tutorial extending the concept further & showing its exact in depth usage if needed.

Ok so people have been commenting & sending me queries on how to remove the Proximity alert , so here is how :


private void removeProximityAlert() {

String context = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager) getSystemService(context);

Intent anIntent = new Intent("com.adnan.proximityalert"); 
PendingIntent operation = 
PendingIntent.getBroadcast(getApplicationContext(), unique_id , anIntent, 0);


Hope this clears up things better !

Second thing which is most confusing is , what is this "com.adnan.proximityalert"?

Well , that is just something you need to declare , so that line in your code should look like this


Please do comment your views about the tutorial & if you have any queries please feel free to drop a comment below !