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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;
public class TestSMS extends MIDlet implements CommandListener,MessageListener,Runnable {
public static int thread = 0;
Display affichage;
Form ecran; // écran
TextField login;
TextField password;
Ticker textDefile;
MessageConnection mc,msg;
BinaryMessage bmsg;
Message rec;
Byte[] data,mesrec1;
String desc,mesrec;
static final Command connCommand = new Command("Envoyer", Command.OK, 0);
static final Command retourCommand = new Command("Retour", Command.BACK, 0);
public TestSMS() {
// TODO Auto-generated constructor stub
}
public void Lancer() throws IOException {
affichage = Display.getDisplay(this);
ecran = new Form("Authentification");
login = new TextField("Login ", "", 15, TextField.ANY);
password = new TextField("Mot de passe", "", 15, TextField.PASSWORD);
ecran.append(login);
ecran.append(password);
ecran.addCommand(connCommand);
ecran.setCommandListener(this);
affichage.setCurrent(ecran);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
try {
Lancer();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Envoyer(String address, String text) throws IOException {
try {
MessageConnection mc =(MessageConnection) Connector.open("sms://+5550001:1234");
TextMessage tm = (TextMessage)mc.newMessage(MessageConnection.TEXT_MESSAGE);
tm.setPayloadText(text);
mc.send(tm);
} catch (IOException e) {e.printStackTrace();}
}
public void commandAction(Command c, Displayable disp) {
String label = c.getLabel();
if (label.equals("Envoyer")) {
thread = 1;
Thread t = new Thread(this);
t.start();
}
else {if (label.equals("Retour")) {
thread = 2;
Thread t1 = new Thread(this);
t1.start();
}
else
{
thread = 3;
Thread t1 = new Thread(this);
t1.start();
}
}
}
public void run() {
if (thread == 1 || thread == 0) {
try {
String textMess = ("#" + login.getString() + "#" + ((TextField)password).getString());
Envoyer(":1234",textMess.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
if (thread == 3) {
notifyIncomingMessage(msg);
}
if (thread == 2) {
try {
Lancer();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void notifyIncomingMessage(MessageConnection mcon) {
Message rec = null;
listen("1234");
if (mc==mcon){
try {
rec = mcon.receive();
if (rec instanceof TextMessage) {
messageRecu((TextMessage)rec);
}
if (rec instanceof BinaryMessage) {
messageRecu((BinaryMessage)rec);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void messageRecu(TextMessage rec2) {
String mesrec =rec2.getPayloadText();
if(mesrec != null){
desc = mesrec.toString();
}
affichage = Display.getDisplay(this);
ecran = new Form("Message Recu");
ecran.append(desc);
ecran.addCommand(retourCommand);
ecran.setCommandListener(this);
affichage.setCurrent(ecran);
}
private void messageRecu(BinaryMessage rec2) {
byte[] mesrec1 =rec2.getPayloadData();
affichage = Display.getDisplay(this);
ecran = new Form("Message Recu");
if(mesrec1 != null){
desc = "Autre";
}
ecran.append(desc.toString());
ecran.addCommand(retourCommand);
ecran.setCommandListener(this);
affichage.setCurrent(ecran);
}
public void listen(String port){
try{
msg =(MessageConnection) Connector.open("sms://:" + port);
msg.setMessageListener(this);
}
catch(Exception e){}
}
} |
Partager