Ch11 code
From SCMAD Book
Contents |
Message Utility (Listing 11.1)
package com.scmadkit.ch11; import javax.wireless.messaging.Message; /** * This class is to get and print timestamp and address of a message to the * system console, and to set a new address to associate with the message. * * @author Ko Ko Naing */ public class MessageUtil { /** * Print the timestamp of the message */ public static void printTimestamp(Message message) { java.util.Date timestamp = message.getTimestamp(); System.out.println("Timestamp of the message : " + timestamp); } /** * Print the address of the message */ public static void printAddress(Message message) { String address = message.getAddress(); System.out.println("Address of the message : " + address); } /** * Set and print a new address to the message */ public static void setAddress(Message message, String address) { System.out.println("The original address of the message is " + message.getAddress()); System.out.println("The address to be set into the message is " + address); message.setAddress(address); System.out.println("The new address of the message is " + message.getAddress()); } }
Text Message Utility (Listing 11.2)
package com.scmadkit.ch11; import javax.wireless.messaging.TextMessage; /** * This class is to get and print text payload of a text message to the system * console, and to set a new text payload into the message. * * @author Ko Ko Naing */ public class TextMessageUtil { /** * Print the text payload of the message */ public static void printTextPayload(TextMessage textMessage) { String payloadText = textMessage.getPayloadText(); System.out.println("Text payload of the message : " + payloadText); } /** * Set and print a text payload to the message */ public static void setTextPayload(TextMessage textMessage, String payloadText) { System.out.println("The original text payload of the message is " + textMessage.getPayloadText()); System.out.println("The text payload to be set into the message is " + payloadText); textMessage.setPayloadText(payloadText); System.out.println("The new text payload of the message is " + textMessage.getPayloadText()); } }
Binary Message Utility (Listing 11.3)
package com.scmadkit.ch11; import javax.wireless.messaging.BinaryMessage; /** * This class is to get and print binary payload of a binary message to the * system console, and to set a new binary payload into the message. * * @author Ko Ko Naing */ public class BinaryMessageUtil { /** * Print the binary payload of the message */ public static void printBinaryPayload(BinaryMessage binaryMsg) { byte[] payloadData = binaryMsg.getPayloadData(); System.out.println("Binary payload of the message : " + payloadData); } /** * Set and print a binary payload to the message */ public static void setBinaryPayload(BinaryMessage binaryMsg, byte[] payloadData) { System.out.println("The original binary payload of the message is " + binaryMsg.getPayloadData()); System.out.println("The binary payload to be set into the message is " + payloadData); binaryMsg.setPayloadData(payloadData); System.out.println("The new binary payload of the message is " + binaryMsg.getPayloadData()); } }
Sending SMS Messages (Listing 11.4)
package com.scmadkit.ch11; import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*; import java.io.IOException; /** * This MIDlet is to send a text message to a recipient device by using SMS * MessageConnection. * * @author Ko Ko Naing */ public class SMSSenderMIDlet extends MIDlet implements CommandListener, Runnable { /** Current display of the application */ Display display; /** UI command to send a message */ Command sendCommand = new Command("Send", Command.ITEM, 1); /** UI command for exit the application */ Command exitCommand = new Command("Exit", Command.EXIT, 2); /** UI box where users can type a message */ TextBox messageBox; /** The address of the recipient device */ String destiDevice; /** The port on the recipient device */ String destiPort; /** SMS message connection to send text messages */ MessageConnection smsConnection = null; /** SMS Connection String */ String connectionString; /** * Create a new instance of a SMSSenderMIDlet */ public SMSSenderMIDlet() { /** Get the address of the recipient device from JAD */ destiDevice = getAppProperty("Destination-Device"); /** Get the port on the recipient device from JAD */ destiPort = getAppProperty("Destination-Port"); display = Display.getDisplay(this); } /** * Signal that the MIDlet has entered the Active state * * @throws MIDletStateChangeException */ protected void startApp() throws MIDletStateChangeException { connectionString = "sms://" + destiDevice + ":" + destiPort; try { /** Open the message connection */ smsConnection = (MessageConnection) Connector .open(connectionString); } catch (IOException ioe) { ioe.printStackTrace(); } messageBox = new TextBox("Enter Message", null, 65535, TextField.ANY); messageBox.addCommand(exitCommand); messageBox.addCommand(sendCommand); messageBox.setCommandListener(this); display.setCurrent(messageBox); } /** * Respond to UI commands from the user * * @param c UI command requested by the user * @param s screen that initiatizes the user request */ public void commandAction(Command c, Displayable s) { try { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if (c == sendCommand) { /** Send the message using a separate thread */ new Thread(this).start(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * Send the message using another Thread so that we don't need to worry * about UI freezing problem */ public void run() { try { /** Create a new text message */ TextMessage textMessage = (TextMessage) smsConnection .newMessage(MessageConnection.TEXT_MESSAGE); textMessage.setPayloadText(messageBox.getString()); /** Send the text message */ smsConnection.send(textMessage); } catch (Exception e) { e.printStackTrace(); } } public void destroyApp(boolean unconditional) { if (smsConnection != null) { try { /** Close the message connection */ smsConnection.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } public void pauseApp() { } }
Receiving SMS messages (Listing 11.5)
package com.scmadkit.ch11; import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*; import java.io.IOException; /** * This MIDlet is to receive text messages by using SMS MessageConnection. * * @author Ko Ko Naing */ public class SMSReceiverMIDlet extends MIDlet implements CommandListener { /** UI command for exit the application */ Command exitCommand = new Command("Exit", Command.EXIT, 1); /** UI Alert to display received text messages */ Alert alert; /** Current display of the application */ Display display; /** The port on which we listen for SMS messages */ String listeningPort; /** SMS message connection to receive text messages */ MessageConnection smsConnection; /** The message received from an SMS message connection */ Message message; /** * Create a new instance of a SMSReceiverMIDlet */ public SMSReceiverMIDlet() { /** Get the port on which we listen for SMS messages */ listeningPort = getAppProperty("Incoming-Port"); display = Display.getDisplay(this); } /** * Signal that the MIDlet has entered the Active state * * @throws MIDletStateChangeException */ public void startApp() { alert = new Alert("SMS Receiver MIDlet"); alert.setTimeout(Alert.FOREVER); alert.addCommand(exitCommand); alert.setCommandListener(this); alert.setString("Listening on port " + listeningPort + "..."); display.setCurrent(alert); /** SMS connection to be read. */ String connectionString = "sms://:" + listeningPort; try { while (true) { if (smsConnection == null) { /** Open the message connection. */ smsConnection = (MessageConnection) Connector .open(connectionString); } message = smsConnection.receive(); if (message != null) { String senderAddress = message.getAddress(); alert.setTitle("From: " + senderAddress); alert.setString(((TextMessage) message).getPayloadText()); display.setCurrent(alert); } } } catch (IOException ioe) { ioe.printStackTrace(); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { if (smsConnection != null) { try { /** Close the message connection */ smsConnection.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } /** * Respond to UI commands from the user * * @param c UI command requested by the user * @param s screen that initiatizes the user request */ public void commandAction(Command c, Displayable s) { try { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } catch (Exception ex) { ex.printStackTrace(); } } }
Receiving SMS messages asynchronously (Listing 11.6)
package com.scmadkit.ch11; import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*; import java.io.IOException; /** * This MIDlet is to receive text messages asynchronously by using SMS * MessageConnection and the MessageListener interface. * * @author Ko Ko Naing */ public class AsynchronousSMSReceiverMIDlet extends MIDlet implements CommandListener, MessageListener, Runnable { /** UI command for exit the application */ Command exitCommand = new Command("Exit", Command.EXIT, 1); /** UI Alert to display received text messages */ Alert alert; /** Current display of the application */ Display display; /** The port on which we listen for SMS messages */ String listeningPort; /** SMS message connection to receive text messages */ MessageConnection smsConnection; /** The message received from an SMS message connection */ Message message; /** * Create a new instance of a HelloWorldSMSSender */ public AsynchronousSMSReceiverMIDlet() { /** Get the port on which we listen for SMS messages */ listeningPort = getAppProperty("Incoming-Port"); display = Display.getDisplay(this); } /** * Signal that the MIDlet has entered the Active state * * @throws MIDletStateChangeException */ public void startApp() { alert = new Alert("Asynchronous SMS Receiver MIDlet"); alert.setTimeout(Alert.FOREVER); alert.addCommand(exitCommand); alert.setCommandListener(this); alert.setString("Listening on port " + listeningPort + "..."); display.setCurrent(alert); /** SMS connection to be read */ String connectionString = "sms://:" + listeningPort; /** Open the message connection */ if (smsConnection == null) { try { /** Open the message connection */ smsConnection = (MessageConnection) Connector .open(connectionString); smsConnection.setMessageListener(this); } catch (IOException ioe) { ioe.printStackTrace(); } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { if (smsConnection != null) { try { /** Close the message connection */ smsConnection.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } /** * Respond to UI commands from the user * * @param c UI command requested by the user * @param s screen that initiatizes the user request */ public void commandAction(Command c, Displayable s) { try { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } catch (Exception ex) { ex.printStackTrace(); } } /** * Called when a new message arrives * * @param messageConnection the connection on which the message is received */ public void notifyIncomingMessage(MessageConnection messageConnection) { new Thread(this).start(); } public void run() { try { message = smsConnection.receive(); if (message != null) { String senderAddress = message.getAddress(); alert.setTitle("From: " + senderAddress); alert.setString(((TextMessage) message).getPayloadText()); display.setCurrent(alert); } } catch (IOException ioe) { ioe.printStackTrace(); } } }
