// SimpleGPSTest2 -- a program that tests your GPS using the Java ME Location API // By Thomas Padron-McCarthy (padrone@lysator.liu.se) // Latest change to this file: March 31, 2009 // No copyright. No warranty. No nothing. Share and enjoy! import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.location.*; public class SimpleGPSTest2 extends MIDlet implements CommandListener { private Display display; private Form form = new Form("Simple GPS Test"); private Command get_lp_command = new Command("Get LP", "Get Location Provider", Command.SCREEN, 1); private Command get_location_command = new Command("Get Location", Command.SCREEN, 0); private Command get_coordinates_command = new Command("Get Coordinates", Command.SCREEN, 1); private Command exit_command = new Command("Exit", Command.EXIT, 2); public SimpleGPSTest2() { } public void destroyApp(boolean unconditional) { } public void pauseApp() { } public void startApp() { display = Display.getDisplay(this); display.setCurrent(form); form.addCommand(get_lp_command); form.addCommand(get_location_command); form.addCommand(get_coordinates_command); form.addCommand(exit_command); form.setCommandListener(this); display.setCurrent(form); } public void commandAction(Command c, Displayable s) { if (c == exit_command) { destroyApp(true); notifyDestroyed(); } else if (c == get_lp_command) { get_lp(); } else if (c == get_location_command) { get_location(); } else if (c == get_coordinates_command) { get_coordinates(); } else { Alert alert = new Alert("Internal error", "This can't happen!", null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } // commandAction private LocationProvider lp; private void get_lp() { try { lp = LocationProvider.getInstance(null); StringItem item = new StringItem("Location Provider: ", "" + lp); form.append(item); } catch(LocationException e) { Alert alert = new Alert("LocationException", "There was a location problem when trying to get a location provider: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } catch(Throwable e) { Alert alert = new Alert("Throwable", "Some other, unexpected problem occured when trying to get a location provider: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } private Location location; private void get_location() { if (lp == null) { Alert alert = new Alert("Wrong!", "You need to get a location provider before you can get a location.", null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } else { Runnable r = new Runnable() { public void run() { try { StringItem waiting_item = new StringItem("Trying to get a location...", ""); form.append(waiting_item); location = lp.getLocation(-1); StringItem location_item = new StringItem("Location: ", "" + location); form.append(location_item); StringItem valid_item = new StringItem("Location isValid(): ", "" + location.isValid()); form.append(valid_item); } catch(LocationException e) { Alert alert = new Alert("LocationException", "There was a location problem when trying to get a location: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } catch(InterruptedException e) { Alert alert = new Alert("InterruptedException", "Reached timeout when trying to get a location: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } catch(SecurityException e) { Alert alert = new Alert("SecurityException", "You weren't allowed to get a location: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } catch(Throwable e) { Alert alert = new Alert("Throwable", "Some other, unexpected problem occured when trying to get a location: " + e, null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } } // run }; // Runnable Thread t = new Thread(r); t.start(); } } // get_location private void get_coordinates() { if (location == null) { Alert alert = new Alert("Wrong!", "You need to get a location before you can get the coordinates.", null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } else if (location.isValid() == false) { Alert alert = new Alert("Wrong!", "You need to get a valid location before you can get the coordinates.", null, null); alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); } else { QualifiedCoordinates qc = location.getQualifiedCoordinates(); double latitude = qc.getLatitude(); double longitude = qc.getLongitude(); double accuracy = qc.getHorizontalAccuracy(); StringItem latitude_item = new StringItem("Latitude: ", "" + latitude); form.append(latitude_item); StringItem longitude_item = new StringItem("Longitude: ", "" + longitude); form.append(longitude_item); StringItem accuracy_item = new StringItem("Accuracy: ", "" + accuracy); form.append(accuracy_item); } } // get_coordinates } // SimpleGPSTest2