Developing proximity alert app for Android - Part 1 of 3


When you need your phone to remind you of doing something, you possibly set an alarm to alert you at specific time. This is useful when you want to be reminded at specific time. However, there are time when you need to be reminded when you arrived at a specific place. This type of reminder is called proximity alert. Android system has built-in proximity alert to achieve this purpose.

To build an Android app with proximity alert, the main ingredient are Broadcast Receiver, Intent, PendingIntent, and AddProximityAlert.

The workflow should be as following: -
1. Register an IntentFilter with a Broadcast Receiver
2. Create an Intent
3. Get a PendingIntent that will perform a broadcast
4. Set proximity alert


The above four steps will register a proximity alert to the system. When your device approaching the target location, system will broadcast the Intent you have created in step 2 above. The broadcast receiver you have registered in step 1 above will receive the broadcast and will fire onReceive event.

STEP 1: To register an IntentFilter with a Broadcast Receiver:

// Code 1
// ======

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
context.registerReceiver(IntentReceiver.class, filter);



In this case, PROX_ALERT_INTENT is a constant for a key to filter your intent


// Code 2
// ======

private static final String PROX_ALERT_INTENT = "com.androidapps.tablet.ProximityAlert";



IntentReceiver is the class extended BroadcastReceiver. It is responsible to listen to the system broadcast with the Intent you are going to create in step 2. Check Code 1 above, IntentReceiver is registered to listen to intent with filter set to PROX_ALERT_INTENT. By setting the filter, IntentReceiver will response only to the intent that matches this filter.


// Code 3
// ======

public class IntentReceiver extends BroadcastReceiver{
	
	@Override
	public void onReceive(Context context, Intent intent) {
	     .....
	     .....
	}
}



STEP 2: Create an Intent:

// Code 4
// ======

Intent mIntent = new Intent(PROX_ALERT_INTENT);
mIntent.putExtra("alert", "Say hi to grandma");



You create an intent with the action set to PROX_ALERT_INTENT, which will be matched by the filter set for IntentReceiver. putExtra method allows you to set information in the intent with “alert” as the key.

STEP 3: Get a PendingIntent that will perform a broadcast:

// Code 5
// ======

PendingIntent proxIntent = PendingIntent.getBroadcast(context, 0, mIntent, 0);



You are creating a pending intent that will broadcast mIntent. Pending intent as the name implied, is the intent that is “pending”, ready to broadcast upon certain event. In this case, the broadcast take place when the device approaching (Lat, Lng) within radius R (See step 4 follows).

STEP 4: Set proximity alert:

// Code 6
// ======

expiryInMillisecond = expiry.getTime();
			
locationMgr.addProximityAlert(getLat(), getLng(), getR(), expiryInMillisecond, proxIntent);



expiry is a variable in Date datatype. In Code 6, you are telling LocationManager to add proximity alert. You are telling the system when the device approaching (Lat,Lng) within radius R, broadcast mIntent set in pending intent proxIntent.

Part 2, Part 3
  1. moretin posted this