私はJ2MEでSMSを受信しようとすると、このコードは何もしません。 startApp()からappを起動すると、メッセージの受信を開始するrun()を呼び出す新しいスレッドが開始されます。見てください。あなたはJavaでスレッドについてまで読む必要がJ2ME SMS応答が応答しない
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) {
}
}
ああ申し訳ありません!とにかくありがとう.. – ruben