| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 
 | import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
 
public class sss extends MIDlet implements CommandListener {
    private Display display;
    private TextField textMesg;
    private TextField dest;
    private Command doneCommand;
    private Command send1Command;
    private Command send2Command;
    private String serverPort;
    private boolean done;
    private MessageConnection sconn;
 
    public sss () throws Exception {
        display = Display.getDisplay(this);
 
        doneCommand = new Command("DONE", Command.SCREEN, 1);
        send1Command = new Command("SEND1", Command.SCREEN, 1);
        send2Command = new Command("SEND2", Command.SCREEN, 1);
 
        serverPort = getAppProperty("serverPort");
    }
 
    public void startApp() {
        try {
            displayBlankForm ();
            sconn = (MessageConnection)
            Connector.open("sms://:" + serverPort);
            done = false;
            new Thread(new SMSServer()).start();
        } catch (Exception e) {
            System.out.print("Error in start");
            e.printStackTrace();
        }
    }
 
    public void pauseApp() {
        done = true;
        try {
            sconn.close();
        } catch (Exception e) {
            System.out.print("Error in pause");
            e.printStackTrace();
        }
    }
 
    public void destroyApp(boolean unconditional) {
        done = true;
        try {
            sconn.close();
        } catch (Exception e) {
            System.out.print("Error in pause");
            e.printStackTrace();
        }
    }
 
    // There are some potentially blocking I/O
    // operations in this callback function.
    // In real world applications, you probably
    // want to move them to a separate thread.
 
    public void commandAction(Command command, Displayable screen) {
        if (command == doneCommand) {
            destroyApp(false);
            notifyDestroyed();
        } else if (command == send1Command) {
            try {
                String addr = "sms://+" + dest.getString();
                MessageConnection conn = (MessageConnection) Connector.open(addr);
                TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                msg.setPayloadText( textMesg.getString() );
                conn.send(msg);
                conn.close();
                displayBlankForm ();
            } catch (Exception e) {
                System.out.println("Error in sending");
                e.printStackTrace ();
            }
        } else if (command == send2Command) {
            try {
                String addr = "sms://+" + dest.getString();
                TextMessage msg = (TextMessage) sconn.newMessage(MessageConnection.TEXT_MESSAGE);
                msg.setAddress ( addr );
                msg.setPayloadText( textMesg.getString() );
                sconn.send(msg);
                displayBlankForm ();
            } catch (Exception e) {
                System.out.println("Error in sending");
                e.printStackTrace ();
            }
        }
    }
 
    private void displayBlankForm () throws Exception {
        Form form = new Form ("WMATester");
        textMesg = new TextField("Message", "", 100, TextField.ANY);
        dest = new TextField("Phone No.", "", 20, TextField.ANY);
        form.append( dest );
        form.append( textMesg );
        form.addCommand(doneCommand);
        form.addCommand(send1Command);
        form.addCommand(send2Command);
        form.setCommandListener(
        (CommandListener) this);
        display.setCurrent(form);
    }
 
    class SMSServer implements Runnable {
        public void run () {
            try {
                while (!done) {
                    Message msg = sconn.receive();
                    if (msg instanceof TextMessage) {
                        TextMessage tmsg = (TextMessage) msg;
                        String msgText = tmsg.getPayloadText();
 
                        // Construct the return message
                        TextMessage rmsg = (TextMessage) sconn.newMessage(MessageConnection.TEXT_MESSAGE);
                        rmsg.setAddress ( tmsg.getAddress() );
                        rmsg.setPayloadText( "Message " + msgText + " is received" );
                        sconn.send(rmsg);
 
                        Alert alert = new Alert ("Received", msgText, null, AlertType.ERROR);
                        alert.setTimeout(Alert.FOREVER);
                        display.setCurrent( alert );
                    } else {
                        throw new Exception("Received is not a text mesg");
                    }
                }
            } catch (Exception e) {
                System.out.println("Error in server receiving");
                e.printStackTrace ();
            }
        }
    }
} | 
Partager