문제

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) {
    }
}
도움이 되었습니까?

해결책

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();

다른 팁

Check this blog for Send & Receiving SMS on specific Port with J2ME Application

it will help you lot for this problem. Thanks

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top