JSR179 LocationAPI

 import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import javax.microedition.location.*; import java.util.Date; import java.io.PrintStream; public class TestLocation extends MIDlet implements CommandListener, LocationListener { private Command exitAppCommand = new Command("Exit", Command.EXIT, 1); private Command startSyncCommand = new Command("Start Sync", Command.SCREEN, 1); private Command startAsyncCommand = new Command("Start Async", Command.SCREEN, 1); private Command stopCommand = new Command("Stop", Command.SCREEN, 1); private Command resetCommand = new Command("Reset GPS", Command.SCREEN, 1); private Command exitOptionsCommand = new Command("Exit Options", Command.EXIT, 1); private Command optionsCommand = new Command("Options", Command.SCREEN, 1); private Form mainForm; private Form optionsForm; private TextField optTimeout; private TextField optWait; int timeout = 1; int wait=1; long startTime; long fixTime; private LocationProvider provider; private int gpsState = 0; TextField AppendNumericField(String name) { TextField f = new TextField(name, "", 6, TextField.NUMERIC); f.setLayout(Item.LAYOUT_SHRINK + Item.LAYOUT_RIGHT); optionsForm.append(f); return f; } void createOptionsForm() { optionsForm = new Form("Options"); optTimeout = AppendNumericField("Timeout"); optWait = AppendNumericField("Wait"); optionsForm.addCommand(exitOptionsCommand); optionsForm.setCommandListener(this); } void createMainForm() { mainForm = new Form("Test GPS"); mainForm.addCommand(startSyncCommand); mainForm.addCommand(startAsyncCommand); mainForm.addCommand(stopCommand); mainForm.addCommand(resetCommand); mainForm.addCommand(optionsCommand); mainForm.addCommand(exitAppCommand); mainForm.setCommandListener(this); } int ReadInteger(TextField t) throws Exception { return Integer.parseInt(t.getString()); } boolean readOptions() { try { int timeout1 = ReadInteger(optTimeout); int wait1 = ReadInteger(optWait); if (timeout1<=0) throw new Exception("Timeout must be greater than 0"); if (wait1<0) throw new Exception("Wait must not be negative"); timeout = timeout1; wait = wait1; return true; } catch (Exception e) { Display.getDisplay(this).setCurrent(new Alert("Error", "Exception " + e, null, AlertType.ERROR)); return false; } } void writeOptions() { optWait.setString(Integer.toString(wait)); optTimeout.setString(Integer.toString(timeout)); } void StartGPS() { startTime = System.currentTimeMillis(); try { if (provider == null) { provider = LocationProvider.getInstance(null); provider.setLocationListener(this, 10, 10, 10); mainForm.append("Started GPS/n"); provider.getLocation(1); } } catch (Exception e) { mainForm.append("StartGPS: " + e + "/n"); } } void showTime() { long time = System.currentTimeMillis(); mainForm.deleteAll(); mainForm.append(new Date(time)+"/n"); mainForm.append("running: " + ((time-startTime)/1000) + "s/n"); mainForm.append("last fix: " + ((fixTime == 0) ? "none/n" : ((time-fixTime)/1000) + "s/n")); } void handleLocation(Location loc) { if (loc.isValid()) { fixTime = System.currentTimeMillis(); QualifiedCoordinates coord = loc.getQualifiedCoordinates(); mainForm.append("lat: " + coord.getLatitude() + "/n"); mainForm.append("lon: " + coord.getLongitude() + "/n"); } else { mainForm.append("Invalid location/n"); } } public void locationUpdated(LocationProvider p, final Location loc) { new Thread() { public void run() { showTime(); handleLocation(loc); } } .start(); } public void providerStateChanged(LocationProvider p, int state) {} class Timer extends Thread { void waitForDeadline(int period) { period *= 1000; mainForm.append("sleep("+period+")/n"); try { Thread.sleep(period); } catch (InterruptedException e) { } } void getLocation(int timeout) { try { mainForm.append("getLocation("+timeout+").../n"); Location loc = provider.getLocation(timeout); showTime(); handleLocation(loc); } catch (Exception e) { showTime(); mainForm.append("Exception " + e + "/n"); return; } } public void run() { mainForm.append("Timer started/n"); while (true) { switch (gpsState) { case 0: // idle try { Thread.sleep(1000); } catch (InterruptedException e) {} break; case 1: // sampling waitForDeadline(wait); getLocation(timeout); break; } } } }; Timer timer; public TestLocation() { createMainForm(); createOptionsForm(); } void stop() { if (provider != null) provider.setLocationListener(null, -1, -1, -1); gpsState = 0; } public void commandAction(Command c, Displayable s) { if (c == exitAppCommand) { destroyApp(true); notifyDestroyed(); } else if (c==startSyncCommand) { stop(); gpsState = 1; } else if (c==startAsyncCommand) { stop(); if (provider != null) provider.setLocationListener(this, timeout + wait, timeout, -1); } else if (c==stopCommand) { stop(); } else if (c==optionsCommand) { writeOptions(); Display.getDisplay(this).setCurrent(optionsForm); } else if (c==resetCommand) { if (provider != null) { mainForm.append("Reset GPS/n"); provider.reset(); } } else if (c==exitOptionsCommand) { if (readOptions()) Display.getDisplay(this).setCurrent(mainForm); } } public void destroyApp(boolean unconditional) { } public void pauseApp() { } public void startApp() { Display.getDisplay(this).setCurrent(mainForm); if (timer == null) { timer = new Timer(); timer.start(); } StartGPS(); } }

你可能感兴趣的:(J2ME,command,exception,timer,null,import,thread)