Saturday, September 6, 2008

Listening for incoming SMS messages

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.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MultipartMessage;
import javax.wireless.messaging.TextMessage;

public class SMSListenerMIDlet extends MIDlet
implements CommandListener, Runnable {
// The port which is listened for incoming messages
private final String PORT = "5000";
private Form mainForm;
private Command startCommand;
private Command stopCommand;
private Command exitCommand;
private MessageConnection connection;
private boolean listening;

public SMSListenerMIDlet() {
mainForm = new Form("SMS Listener");
startCommand = new Command("Start listening", Command.ITEM, 0);
mainForm.addCommand(startCommand);
stopCommand = new Command("Stop listening", Command.ITEM, 1);
mainForm.addCommand(stopCommand);
exitCommand = new Command("Exit", Command.EXIT, 1);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
}

public void startApp() {
// The initial display is the main form
Display.getDisplay(this).setCurrent(mainForm);
}

public void pauseApp() {
// No implementation required
}

public void destroyApp(boolean unconditional) {
// Stop listening
stopListening();
}

public void commandAction(Command command, Displayable displayable) {
if (command == exitCommand) {
// Exit the MIDlet
destroyApp(true);
notifyDestroyed();
} else if (command == startCommand) {
startListening();
} else if (command == stopCommand) {
stopListening();
}
}

private void startListening() {
// If we are already listening, no need to start again
if (listening) {
return;
}
try {
// Open the connection to the specified port
connection = (MessageConnection)Connector.open("sms://:" + PORT);
} catch (IOException ex) {
return;
}
// Create a listener thread and start listening
Thread listenerThread = new Thread(this);
listening = true;
listenerThread.start();
mainForm.append("Listener started.\n");
}

private void stopListening() {
// If we are not listening, no need to do anything
if (!listening) {
return;
}
if (connection != null) {
try {
// Close the message connection
connection.close();
connection = null;
} catch (IOException ex) {
// TODO: Exception handling
}
}
listening = false;
mainForm.append("Listener stopped.\n");
}

public void run() {
while (listening) {
try {
// Receive all incoming messages to the specified port. The
// receive() method will block until there is a message
// available.
Message message = connection.receive();
if (message != null) {
mainForm.append("Message received.\n");
processMessage(message);
}
} catch (IOException ex) {
// Stop listening
stopListening();
}
}
}

private void processMessage(Message message) {
if (message instanceof TextMessage) {
processTextMessage((TextMessage)message);
} else if (message instanceof BinaryMessage) {
processBinaryMessage((BinaryMessage)message);
} else if (message instanceof MultipartMessage) {
processMultipartMessage((MultipartMessage)message);
}
}

private void processTextMessage(TextMessage message) {
String text = message.getPayloadText();
StringItem textItem = new StringItem("Text", text);
mainForm.append(textItem);
}
/**
* Processes a binary message.
*/
private void processBinaryMessage(BinaryMessage binaryMessage) {
// Not implemented
}
/**
* Processes a multipart message.
*/
private void processMultipartMessage(MultipartMessage multipartMessage) {
// Not implemented
}
}

1 comment:

Sweet Sweet said...

Thanks you for your code , but it is not working in real phone to phone.
If you know please share your knowledge

search engine

Custom Search