Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
How To Send SMS In J2ME
by Manoj Alwis
Hi friend’s ,this month article I’ll demonstrates how to send a text SMS in J2ME. In this example, the user first enters a phone number to a text field. He or she may also fetch the number from the address book. After that, the user enters the text to be sent. Then, by selecting Send, the SMS is sent to the specified number.
This is a complete example MIDlet, In here the most important methods are handleSendCommand(), prepareSMS(), and sendSMS().
Source file
package src;
import java.io.IOException;
import java.io.InterruptedIOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
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.midlet.MIDlet;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
//By Manoj Alwis ........
public class SMSMIDlet extends MIDlet implements CommandListener {
private Command sendCommand;
private Command exitCommand;
private Form mainForm;
private TextField smsNumber;
private TextField smsText;
private MessageConnection connection;
private Alert alert;
private Display display;
/**
* Constructor. Constructs the object and initializes displayables.
*/
public SMSMIDlet() {
mainForm = new Form("SMS Example");
smsNumber = new TextField("Phone number", null, 20,
TextField.PHONENUMBER);
mainForm.append(smsNumber);
smsText = new TextField("Text", null, 160, TextField.PLAIN);
mainForm.append(smsText);
sendCommand = new Command("Send", Command.ITEM, 0);
mainForm.addCommand(sendCommand);
exitCommand = new Command("Exit", Command.EXIT, 0);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
}
/**
* From MIDlet.
* Called when the MIDlet is started.
*/
public void startApp() {
// The initial display is the main form
Display.getDisplay(this).setCurrent(mainForm);
}
/**
* From MIDlet.
* Called to signal the MIDlet to enter the Paused state.
*/
public void pauseApp() {
// No implementation required
}
/**
* From MIDlet.
* Called to signal the MIDlet to terminate.
*/
public void destroyApp(boolean unconditional) {
if (connection != null) {
try {
connection.close();
} catch (IOException ex) {
}
}
}
/**
* From CommandListener.
* Called by the system to indicate that a command has been invoked on a
* particular displayable.
*/
public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
// Exit the MIDlet
destroyApp(true);
notifyDestroyed();
} else if (command == sendCommand) {
handleSendCommand();
}
}
private void handleSendCommand() {
try {
// Open the connection
connection = (MessageConnection)Connector.open("sms://:5000");
} catch (IOException ex) {
alert = new Alert("Alert");
alert.setString("Unable to connect to Station because of network problem");
alert.setTimeout(2000);
display.setCurrent(alert);
}
TextMessage message = prepareSMS();
sendSMS(message);
}
/**
* Sets the destination address and payload text for the text SMS.
*/
private TextMessage prepareSMS() {
// Prepare the text message
TextMessage message = (TextMessage)connection.newMessage(
MessageConnection.TEXT_MESSAGE);
// Set the destination address
String number = "sms://" + smsNumber.getString();
message.setAddress(number);
// Obtain the specified text and set it as the payload
String text = smsText.getString();
message.setPayloadText(text);
return message;
}
/**
* Sends a text SMS.
*/
private void sendSMS(final TextMessage message) {
// Send the message on its own thread of execution
Thread smsThread = new Thread() {
public void run() {
try {
connection.send(message);
mainForm.append("Message sent.");
} catch (InterruptedIOException ex) {
} catch (IOException ex) {
}
} catch (SecurityException ex) {
}
}
};
smsThread.start();
}
}

Post new comment