J2ME短信接收无响应
当我尝试在 j2me 中接收短信时,此代码什么也不做。当应用程序从 startApp() 启动时,将启动一个新线程,该线程调用 run() 并开始侦听消息。请看一下。
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) {
}
}
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) {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要阅读有关 Java 线程的知识。
目前,您正在启动一个不执行任何操作的新线程。
请参阅 空线程构造函数的 Javadoc:
您的 MIDlet 实现了 Runnable,因此您需要将其传递到线程中。
试试这个:
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:
Your MIDlet implements
Runnable
so you need to pass that into the thread.Try this instead: