Wednesday, April 27, 2011

J2ME SMS : Sending SMS in J2ME



************************************************** ***
J2ME SMS
Sending SMS in J2ME


insert this line into JAD file:
Port-SMS: 50056


************************************************** ****/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.*;
import javax.microedition.io.*;
import java.io.*;

public class SendSMSextends MIDlet
implements CommandListener, Runnable {

private Display display;
private Command cmdExit, cmdContinue, cmdBack, cmdSend;
private Displayable prevScreen;
private TextBox tfNum, tfText;
private String port;
private Thread thread;
private Alert alert;

private final String INVALID_PHONE =
"Nomor yang dimasukkan tidak absah";

public SendSMS() {
display = Display.getDisplay(this);

port = "50056";
if(getAppProperty("Port-SMS") != null)
port = getAppProperty("Port-SMS");

cmdExit = new Command("Exit", Command.EXIT, 1);
cmdContinue = new Command("Continue", Command.SCREEN, 1);
cmdBack = new Command("Back", Command.BACK, 1);
cmdSend = new Command("Send", Command.SCREEN, 1);

alert = new Alert(null);
alert.setTimeout(3000);
}

public void startApp() {
tfNum = generatePhoneInput();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdBack) {
display.setCurrent(tfNum);
} else if (c == cmdContinue) {
if (!isValidPhoneNumber(tfNum.getString())) {
alert.setType(AlertType.ERROR);
alert.setString(INVALID_PHONE);
display.setCurrent(alert,tfNum);
} else {
tfText = generateMessageInput();
}
} else {
thread = new Thread(this);
thread.start();
}
}

public void run() {
MessageConnection conn = null;
String address= "sms://" + tfNum.getString() + ":" + port;
try {
conn = (MessageConnection) Connector.open(address);
TextMessage msg = (TextMessage)
conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setAddress(address);
msg.setPayloadText(tfText.getString());
conn.send(msg);
alert.setString("Sending to " +
address.substring(6));
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

public TextBox generatePhoneInput() {
TextBox tfPhone = new TextBox("Number?", null,
15, TextField.PHONENUMBER);
tfPhone.addCommand(cmdExit);
tfPhone.addCommand(cmdContinue);
tfPhone.setCommandListener(this);
display.setCurrent(tfPhone);
return tfPhone;
}

public TextBox generateMessageInput() {
TextBox tfMessage = new TextBox("Text Message", null,
500, TextField.ANY);
tfMessage.addCommand(cmdBack);
tfMessage.addCommand(cmdSend);
tfMessage.setCommandListener(this);
display.setCurrent(tfMessage);
return tfMessage;
}

private static boolean isValidPhoneNumber(String number) {
char[] chars = number.toCharArray();
if (chars.length == 0) {
return false;
}
int startPos = 0;
//+
if (chars[0] == '+') {
startPos = 1;
}
for (int i = startPos; i < chars.length; ++i) { if (!Character.isDigit(chars[i])) { return false; } } return true; } }

No comments:

Post a Comment

Search This Blog

Total Pageviews