Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend


The following example demonstrates how to receive a text message (SMS) in an application. Study the code—the most important parts are in the startApp() method, in which the message itself is read from the system.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import javax.wireless.messaging.*;

public class PushMIDlet extends MIDlet implements CommandListener {

 private Display display;
 private Command coExit;
 private MessageConnection mc = null;
 private Message me;
 private Alert alert;
 private int port = 200;

 public PushMIDlet(){
  display = Display.getDisplay(this);
  coExit = new Command("Exit", Command.EXIT, 1);
  alert = new Alert("Message");

  protected void destroyApp(boolean p0){


  protected void pauseApp() {


  protected void startApp() throws MIDletStateChangeException {
  String connections[] = PushRegistry.listConnections(true);
  if (connections == null || connections.length == 0){
  else {
    mc = (MessageConnection)Connector.open("sms://:"+port);
    me = mc.receive();

    if (me != null && me instanceof TextMessage){
     alert.setTitle("Message from " + me.getAddress());

   } catch (IOException ioe){
    System.out.println("Error in receiving message.");


  public void commandAction(Command c, Displayable dp) {
   if (c==coExit){


And the jad file should look something like this:

MIDlet-1: PushMIDlet, PushMIDlet.png, PushMIDlet
MIDlet-Jar-Size: 1835
MIDlet-Jar-URL: PushMIDlet.jar
MIDlet-Name: PushMIDlet
MIDlet-Push-1: sms://:200, PushMIDlet, *
MIDlet-Vendor: Mikko Kontio
MIDlet-Version: 1.0
MicroEdition-Configuration: CLDC-1.0
MicroEdition-Profile: MIDP-2.0

To run the example with WTK 2.0, you must create a new project, write the code, set the PushRegistry settings, and build the project. Then, you can run it via OTA (Over-The-Air) from the Project menu. After successfully installing the application, you can send an SMS (a text message) by choosing File, Utilities and then opening the WMA (Wireless Messaging API) console.

  • + Share This
  • 🔖 Save To Your Account