For information about the design and functionality of the MIDlet, see section Design.
The MIDlet consists of a single class, ProximityReminder
.
To create the MIDlet:
Create the ProximityReminder.java
class file.
Import the required
classes and packages, initialize the UI elements and Commands
, and define the standard startApp
, pauseApp
, and destroyApp
methods without any additional
configurations.
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.location.*; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; public class ProximityReminder extends MIDlet implements CommandListener, ProximityListener { private Alert timedAlert; private Displayable currentDisplay; private Command ExitCmd; private Command OptionsCmd; private Form form; // Menu private List OptionsMenu; private String error = ""; private Location currentLocation = null; public void startApp() { initialize(); } public void pauseApp() { //Nothing to do } public void destroyApp(boolean unconditional) { //Nothing to do }
Add a Form
and two Commands
when the MIDlet is
started.
private void initialize() { getDisplay().setCurrent(get_form()); SetCurrDisplay(get_form()); currentDisplay = getDisplay().getCurrent(); } public Form get_form() { if (form == null) { form = new Form(null); form.addCommand(get_OptionsCmd()); form.addCommand(get_exitCmd()); } form.setTicker(new Ticker("PROXIMITY REMINDER")); form.setCommandListener(this); return form; } // ... public Display getDisplay() { return Display.getDisplay(this); } boolean SetCurrDisplay(Displayable currDisp) { if (currDisp == null) { if (currentDisplay == null) return false; else currDisp = currentDisplay; } else { currentDisplay = currDisp; } getDisplay().setCurrent(currentDisplay); return true; } public void exitMIDlet() { destroyApp(true); notifyDestroyed(); }
Handle the location
detection and exit Commands
.
// COMMANDS public Command get_exitCmd() { if (ExitCmd == null) { ExitCmd = new Command("Exit", Command.EXIT, 1); } return ExitCmd; } // COMMANDS public Command get_OptionsCmd() { if (OptionsCmd == null) { OptionsCmd = new Command("Options", Command.OK, 1); } return OptionsCmd; } public List get_Options() { if (OptionsMenu == null) { OptionsMenu = new List("Select Option", Choice.IMPLICIT, new String[] { "Add Proximity to Current Location", "Cancel" }, new Image[] { null, null }); OptionsMenu.setCommandListener(this); OptionsMenu.setSelectedFlags(new boolean[] { true, false }); } return OptionsMenu; } public void commandAction(Command command, Displayable displayable) { if (displayable == form) { if (command == ExitCmd) { exitMIDlet(); } else if (command == OptionsCmd) { getDisplay().setCurrent(get_Options()); } } if (displayable == OptionsMenu) { if (command == OptionsMenu.SELECT_COMMAND) { switch (OptionsMenu.getSelectedIndex()) { case 0: new Thread(new Runnable() { public void run() { getCurrentLocation(); } }).start(); // flow through case 1: getDisplay().setCurrent(get_form()); break; } } else if (command == ExitCmd) { exitMIDlet(); } } }
Create methods
for detecting the current location coordinates and for adding a ProximityListener
to listen to changes in
the device location.
// fetches the current location and adds the current location to proximity void getCurrentLocation() { try { // Get the provider with default criteria LocationProvider provider = LocationProvider.getInstance(null); if (provider != null) { // fetch the current location currentLocation = provider.getLocation(-1); } } catch (Exception e) { addError(e); } addproximity(); } // Adds the current Location for Proxmity Updates void addproximity() { try { if (currentLocation != null) { // get the current latitude, langitude and altitude float alt = currentLocation.getQualifiedCoordinates().getAltitude(); double lat = currentLocation.getQualifiedCoordinates().getLatitude(); double lon = currentLocation.getQualifiedCoordinates().getLongitude(); Coordinates cord1 = new Coordinates(lat, lon, alt); // Adds the current coordinates for proximity updates when the // device is within the proximity radius of 500 meters LocationProvider.addProximityListener(this, cord1, 500.0f); } } catch (SecurityException e) { addError(e); } catch (Exception e) { addError(e); } } public void addError(Exception e) { e.printStackTrace(); error += e.getMessage() + "\n"; }
Use the ProximityListener.monitoringStateChanged
method
to listen when the state of the proximity monitoring has changed.
// This function will be called when the monitoring state changes public void monitoringStateChanged(boolean aState) { if (aState) { timedAlert = new Alert("Proximity Status", " Active", null, AlertType.INFO); timedAlert.setTimeout(5000); } else { timedAlert = new Alert("Proximity Status", " NOT Active", null, AlertType.INFO); timedAlert.setTimeout(5000); } }
proximityEvent
for handling the situations
when the device proximity event is activated. Display the location
information from the event to the user using an Alert
.// This function will be called when the Proximity event fires public void proximityEvent(Coordinates aCord, Location aLoc) { // Displays the information using an alert timedAlert = new Alert("Proximity Update", "You are now near Location: lat = " + aCord.getLatitude() + " lon =" + aCord.getLongitude(), null, AlertType.INFO); timedAlert.setTimeout(5000); currentDisplay = getDisplay().getCurrent(); getDisplay().setCurrent(timedAlert, currentDisplay); } }