






































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































1
Sent through a store-and-forward network
Messages are sent and received using a MessageConnection The javax.wireless.messaging package contains classes for sending and receiving messages Message is an interface These are subinterfaces
Sending a message Receiving a message
Select the Wireless Toolkit Go to the Tools & Extensions tab Click the Open Utilities button
Select and click Launch
E.g. +
E.g. +5550000:
import javax.microedition.midlet.; import javax.microedition.lcdui.; import javax.wireless.messaging.; import javax.microedition.io.Connector; public class SendMidlet extends MIDlet implements CommandListener, Runnable{ private Form mForm; private StringItem lblMessage; private TextField txtDest, txtMsg; ... } Importing packages required for messaging Displays a success / failure message To specify the destination number For entering the message to be sent*
public void commandAction(Command c, Displayable s) { if (c.getCommandType() == Command.EXIT) { notifyDestroyed(); } else { lblMessage.setText("Sending SMS..."); Thread t = new Thread(this); t.start(); } } Starts a new thread so that our GUI doesn’t hang when we send a message – this is why our MIDlet implements Runnable
public void run(){ try { MessageConnection conn = (MessageConnection) Connector.open("sms://" + txtDest.getString()); TextMessage txtMessage = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); txtMessage.setPayloadText(txtMsg.getString()); conn.send(txtMessage); lblMessage.setText("SMS sent successfully"); conn.close(); } catch(Exception ex) { lblMessage.setText("Message could not be sent"); } } The Connector creates the correct connection for the given protocol We are sending an SMS in text format Sets the text to be sent Sends the message Whenever we deal with external resources, we should use try…catch
Sends a message Receives a message
import javax.microedition.midlet.; import javax.microedition.lcdui.; import javax.wireless.messaging.; import javax.microedition.io.Connector; public class ReceiveMidlet extends MIDlet implements CommandListener, Runnable{ private Form mForm; private StringItem lblMessage; private TextField txtPort; private boolean finished; ... } Importing packages required for messaging Displays received messages Used to keep displaying received messages until MIDlet is closed For entering the port to listen on*