1 2 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
|
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import java.io.IOException;
public class SMSMIDlet
extends MIDlet
implements CommandListener, Runnable {
private Sender mSender = null;
private Thread mReceiver = null;
private Command mExitCommand = new Command("Exit", Command.EXIT, 2);
private Command mRedCommand = new Command("Send Red", Command.SCREEN, 1);
private Command mBlueCommand = new Command("Send Blue", Command.SCREEN, 1);
private Display mDisplay = null;
protected ImageItem mColorSquare = null;
protected Image [] mImages = new Image[2];
protected Image waitImage = null;
private String mPort = "1234";
private TextField mNumberEntry= null;
private Form mForm = null;
private String mSenderAddress = null;
public SMSMIDlet() {
mSender = Sender.getInstance();
}
public void commandAction(javax.microedition.lcdui.Command c,
javax.microedition.lcdui.Displayable d) {
if (c == mExitCommand) {
if (!mSender.isSending()) {
destroyApp(true);
notifyDestroyed();
}
} else if (c == mRedCommand) {
String dest = mNumberEntry.getString();
if (dest.length() > 0)
mSender.sendMsg(dest, mPort, "red");
} else if (c == mBlueCommand) {
String dest = mNumberEntry.getString();
if (dest.length() > 0)
mSender.sendMsg(dest, mPort, "blue");
}
}
protected void destroyApp(boolean param) {
try {
mEndNow = true;
conn.close();
} catch (IOException ex) {
System.out.println("destroyApp caught: ");
ex.printStackTrace();
}
}
protected void pauseApp() {
}
protected void startApp() {
if (mForm == null) {
mForm = new Form("SMSMIDlet");
mNumberEntry = new TextField("Connect to:",
null, 256, TextField.PHONENUMBER);
try {
mImages[0] = Image.createImage("/red.png");
mImages[1] = Image.createImage("/blue.png");
waitImage = Image.createImage("/wait.png");
} catch (Exception ex) {
System.out.println("startApp caught: ");
ex.printStackTrace();
}
mColorSquare = new ImageItem(null, waitImage,ImageItem.
LAYOUT_DEFAULT, "colored square");
mForm.append(mNumberEntry);
mForm.append(mColorSquare);
mForm.addCommand(mExitCommand);
mForm.addCommand(mRedCommand);
mForm.addCommand(mBlueCommand);
mForm.setCommandListener(this);
}
Display.getDisplay(this).setCurrent(mForm);
startReceive();
}
private void startReceive() {
if (mReceiver != null)
return;
// Start receive thread
mReceiver = new Thread(this);
mReceiver.start();
}
private boolean mEndNow = false;
private MessageConnection conn = null;
public void run() {
Message msg = null;
String msgReceived = null;
conn = null;
mEndNow = false;
/** Check for sms connection. */
try {
conn = (MessageConnection) Connector.open("sms://:" + mPort);
msg = conn.receive();
while ((msg != null) && (!mEndNow)) {
if (msg instanceof TextMessage) {
msgReceived = ((TextMessage)msg).getPayloadText();
if (msgReceived.equals("red")) {
Display.getDisplay(this).callSerially(new SetRed());
} else if (msgReceived.equals("blue")) {
Display.getDisplay(this).callSerially(new SetBlue());
}
}
msg = conn.receive();
}
} catch (IOException e) {
// Normal exit when connection is closed
}
}
class SetRed implements Runnable {
Display disp = null;
public void run() {
mColorSquare.setImage(mImages[0]);
}
}
class SetBlue implements Runnable {
public void run() {
mColorSquare.setImage(mImages[1]);
}
}
} |
Partager