I used the following code:
Code:
public class StubMIDlet extends MIDlet implements CommandListener,
MessageListener {
private Display display;
private Form mainForm;
private TextField logField;
private DataOutputStream logStream;
private final String LOG_PATH;
private static final String LOG_FILE = "JavaMEStubLog.txt";
private final String MIDLET_CLASS_NAME;
private static final String SMS_CONNECTION_URL = "sms://:16013";
private static final String ALLOWED_SENDER_FILTER = "*";
private static final Command EXECUTE_COMMAND =
new Command("Execute snippet", Command.ITEM, 0);
private static final Command EXIT_COMMAND =
new Command("Exit", Command.EXIT, 0);
private static final Command REGISTER_CONNECTION_COMMAND =
new Command("Register connection", Command.ITEM, 0);
private static final Command UNREGISTER_CONNECTION_COMMAND =
new Command("Unregister connection", Command.ITEM, 0);
private static final Command REGISTER_TIMER_ALARM_COMMAND =
new Command("Register timer alarm", Command.ITEM, 0);
private Vector availableConnections = new Vector();
public StubMIDlet() {
LOG_PATH = System.getProperty("fileconn.dir.photos");
MIDLET_CLASS_NAME = this.getClass().getName();
display = Display.getDisplay(this);
setupMainForm();
setupLogFile();
}
private void setupMainForm() {
mainForm = new Form("StubMIDlet");
logField = new TextField("Log", null, Short.MAX_VALUE, TextField.PLAIN);
mainForm.append(logField);
mainForm.addCommand(REGISTER_CONNECTION_COMMAND);
mainForm.addCommand(UNREGISTER_CONNECTION_COMMAND);
mainForm.addCommand(REGISTER_TIMER_ALARM_COMMAND);
mainForm.addCommand(EXIT_COMMAND);
mainForm.setCommandListener(this);
}
private void setupLogFile() {
String fcVersion =
System.getProperty("microedition.io.file.FileConnection.version");
if (fcVersion == null) {
printString("The device doesn't support file logging.\n" +
"Log output is not available.");
return;
}
try {
FileConnection fconn =
(FileConnection)Connector.open(LOG_PATH + LOG_FILE);
if (!fconn.exists()) {
fconn.create();
}
logStream = fconn.openDataOutputStream();
fconn.close();
} catch (IOException ex) {
printString("The log file could not be set up.\n" +
"Log output is not available.");
return;
}
}
private void executeSnippet() {
String text = "Snippet executed";
printString(text);
try {
logPrintString(text);
} catch (IOException ex) {
printString(ex.getMessage());
}
displayNote(text);
}
private void registerSMSConnection() {
printString("Registering the connection...");
try {
PushRegistry.registerConnection(SMS_CONNECTION_URL,
MIDLET_CLASS_NAME, ALLOWED_SENDER_FILTER);
printString("Registration is complete!");
// Update the inbound connections
clearConnections();
getPushRegistryConnections();
} catch (ClassNotFoundException ex) {
printString(ex.toString());
printString("Registration failed.");
} catch (IOException ex) {
printString(ex.toString());
printString("Registration failed.");
}
}
private void unregisterSMSConnection() {
printString("Unregistering the connection...");
PushRegistry.unregisterConnection(SMS_CONNECTION_URL);
printString("Unregistration is complete!");
}
private void registerTimerAlarm(long timePeriodToAutoStart) {
// Set the launch time to current time + the specified period
long timeToWakeUp = System.currentTimeMillis() + timePeriodToAutoStart;
printString("Registering the timer alarm...");
try {
PushRegistry.registerAlarm(MIDLET_CLASS_NAME, timeToWakeUp);
printString("Alarm is registered!");
} catch (ClassNotFoundException ex) {
printString(ex.getMessage());
printString("Alarm registration failed.");
} catch (ConnectionNotFoundException ex) {
printString(ex.getMessage());
printString("Alarm registration failed.");
}
}
private void clearConnections() {
if (availableConnections != null) {
while (!availableConnections.isEmpty()) {
MessageConnection messageConnection =
(MessageConnection) availableConnections.firstElement();
if (messageConnection != null) {
try {
messageConnection.setMessageListener(null);
messageConnection.close();
} catch (IOException ex) {
printString(ex.toString());
}
}
availableConnections.removeElementAt(0);
}
}
}
private void getPushRegistryConnections() {
String[] connections;
connections = PushRegistry.listConnections(false);
if (connections.length != 0) {
printString("List of available connections: ");
for (int i = 0; i < connections.length; i++) {
try {
MessageConnection mc =
(MessageConnection) Connector.open(connections[i]);
printString("(" + i + ") - " + connections[i]);
mc.setMessageListener(this);
availableConnections.addElement(mc);
} catch (SecurityException ex) {
printString("Connection failed.");
printString(ex.getMessage());
} catch (IOException ex) {
printString("Connection failed.");
printString(ex.getMessage());
}
}
}
}
private void displayNote(String text) {
Alert infoNote = new Alert("Info", text, null, AlertType.INFO);
infoNote.setTimeout(3000);
display.setCurrent(infoNote, mainForm);
}
private void printString(String text) {
logField.insert(text + "\n", logField.size());
}
private void logPrintString(String text) throws IOException {
if (logStream == null) {
return;
}
logStream.writeChars(text + "\n");
logStream.flush();
}
private void exit() {
try {
if (logStream != null) {
logStream.close();
}
} catch (IOException ex) {
printString(ex.getMessage());
}
notifyDestroyed();
}
public void startApp() {
// The initial display is the main form
display.setCurrent(mainForm);
}
public void pauseApp() {
// No implementation required
}
public void destroyApp(boolean unconditional) {
// No implementation required
}
public void commandAction(Command command, Displayable displayable) {
if (command == EXIT_COMMAND) {
exit();
} else if (command == REGISTER_CONNECTION_COMMAND) {
registerSMSConnection();
} else if (command == UNREGISTER_CONNECTION_COMMAND) {
unregisterSMSConnection();
} else if (command == REGISTER_TIMER_ALARM_COMMAND) {
registerTimerAlarm(30000);
} else if (command == EXECUTE_COMMAND) {
executeSnippet();
}
}
public void notifyIncomingMessage(MessageConnection conn) {
}
}