J2ME SMS receive not responding

903 views Asked by At

When I try to receive sms in j2me this code just does nothing. When app is launched from startApp() a new thread is started which calls run() where it starts listening for a message. Please have a look.

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.*;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

/**
 *
 */
public class Receiver extends MIDlet implements Runnable {

    Display display;
    Alert showMessage = new Alert("Msg", "Checking inbox..", null, AlertType.INFO);

    public void startApp() {
        Thread t = new Thread();
        t.start();
    }

    public void run() {
        try {
            // Time to receive one.
//get reference to MessageConnection object
            showMessage.setTimeout(Alert.FOREVER);
            display.getDisplay(this).setCurrent(showMessage);
            MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
//set message listener
            conn.setMessageListener(new MessageListener() {

                public void notifyIncomingMessage(MessageConnection conn) {
                    try {
                        Message msg = conn.receive();
                        //do whatever you want with the message
                        if (msg instanceof TextMessage) {
                            TextMessage tmsg = (TextMessage) msg;
                            String s = tmsg.getPayloadText();
                            System.out.println(s);
                            //showMessage.setTimeout(Alert.FOREVER);
                            showMessage.setString(s);
                            showMessage.setTitle("Welcome");
                            display.setCurrent(showMessage);

                        } else if (msg instanceof BinaryMessage) {
                            System.out.println("inside else if");
                        } else {
                            System.out.println("inside else");
                        }
                    } catch (Exception e) {
                    }
                }
            });
        } catch (Exception e) {
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}
1

There are 1 answers

0
funkybro On BEST ANSWER
Thread t = new Thread();
t.start();

You need to read up about threading in Java.

Currently, you're starting a new thread which does nothing.

See the Javadoc for the empty Thread constructor:

Threads created this way must have overridden their run() method to actually do anything.

Your MIDlet implements Runnable so you need to pass that into the thread. Try this instead:

new Thread(this).start();