FileDocCategorySizeDatePackage
TimeMIDlet.javaAPI DocExample5108Sun Nov 04 15:21:26 GMT 2001ora.ch6

TimeMIDlet

public class TimeMIDlet extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener, Runnable

Fields Summary
private javax.microedition.lcdui.Display
display
private javax.microedition.lcdui.Form
addressForm
private javax.microedition.lcdui.Form
connectForm
private javax.microedition.lcdui.Form
displayForm
private javax.microedition.lcdui.Command
backCommand
private javax.microedition.lcdui.Command
exitCommand
private javax.microedition.lcdui.Command
okCommand
private javax.microedition.lcdui.StringItem
messageLabel
private javax.microedition.lcdui.TextField
serverName
Constructors Summary
Methods Summary
public voidcommandAction(javax.microedition.lcdui.Command cmd, javax.microedition.lcdui.Displayable d)

        if (cmd == okCommand) {
            Thread t = new Thread(this);
            t.start();
        } else if (cmd == backCommand) {
            display.setCurrent(addressForm);
        } else if (cmd == exitCommand) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException ex) {
            }
            notifyDestroyed();
        }
    
protected voiddestroyApp(boolean unconditional)

    
private voidinitialize()

        display = Display.getDisplay(this);

        // Commands
        exitCommand = new Command("Exit", Command.EXIT, 0);
        okCommand = new Command("OK", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 0);

        // The address form
        addressForm = new Form("Network Time");
        serverName = new TextField("Time Server name:", "tock.usno.navy.mil",
                                            256, TextField.ANY);
        addressForm.append(serverName);
        addressForm.addCommand(okCommand);
        addressForm.addCommand(exitCommand);
        addressForm.setCommandListener(this);

        // The connect form
        connectForm = new Form("Sending");
        messageLabel = new StringItem(null,
                    "Sending the datagram...\nPlease wait.");
        connectForm.append(messageLabel);
        connectForm.addCommand(backCommand);
        connectForm.setCommandListener(this);

        // The display form
        displayForm = new Form("Server Reply");
        messageLabel = new StringItem(null, null);
        displayForm.append(messageLabel);
        displayForm.addCommand(backCommand);
        displayForm.setCommandListener(this);
    
protected voidpauseApp()

    
public voidrun()

        DatagramConnection conn = null;
        display.setCurrent(connectForm);

        try {
            // Build the name string for the Connector open method
            String server = serverName.getString();
            String name = "datagram://" + server + ":" + 13;
            conn = (DatagramConnection)Connector.open(name,
                                    Connector.READ_WRITE, false);

            // Build and send an empty datagram
            Datagram dg = conn.newDatagram(10);
            dg.setData("Hello".getBytes(), 0, 5);
            conn.send(dg);

            // Receive the reply
            Datagram rdg = conn.newDatagram(512);
            conn.receive(rdg);
            messageLabel.setText(new String(rdg.getData(), 0, rdg.getLength()));
            display.setCurrent(displayForm);

        } catch (InterruptedIOException iex) {
            display.callSerially(new Runnable() {
                public void run() {
                    Alert alert = new Alert("No Reply",
                        "No reply was received.\n" +
                        "Please check the server address and try again.", null,
                        AlertType.ERROR);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert, addressForm);
                }
            });
            return;
        } catch (Exception ex) {
            display.callSerially(new Runnable() {
                public void run() {
                    Alert alert = new Alert("Invalid Address",
                        "The supplied address is invalid\n" +
                        "Please correct it and try again.", null,
                        AlertType.ERROR);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert, addressForm);
                }
            });
            return;
        } catch (Error err) {
            System.out.println(err);
            err.printStackTrace();
        }
    
protected voidstartApp()

        if (display == null) {
            initialize();
            display.setCurrent(addressForm);
        }