嗨朋友们,我正在尝试阅读传入的短信,但是会收到这样的警告。调用可疑方法:java.lang.String。(String)位于:mypackage.MyApp $ ListeningThread.run()
这是我的代码
public class MyApp extends UiApplication {
//private ListeningThread listener;
public static void main(String[] args) {
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
}
public MyApp() {
invokeAndWait(new Runnable() {
public void run() {
ListeningThread listener = new ListeningThread();
listener.start();
}
});
pushScreen(new MyScreen());
}
private static class ListeningThread extends Thread {
private boolean _stop = false;
private DatagramConnection _dc;
public synchronized void stop() {
_stop = true;
try {
_dc.close(); // Close the connection so the thread returns.
} catch (IOException e) {
System.err.println(e.toString());
}
}
public void run() {
try {
_dc = (DatagramConnection) Connector.open("sms://");
for (;;) {
if (_stop) {
return;
}
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
String address = new String(d.getAddress());
String msg = new String(d.getData());
if(msg.startsWith("START")){
Dialog.alert("hello");
}
System.out.println("Message received: " + msg);
System.out.println("From: " + address);
System.exit(0);
}
} catch (IOException e) {
System.err.println(e.toString());
}
}
}
}
请纠正我错在哪里。我可以给我一些代码来读取黑莓中传入的短信内容。
答案 0 :(得分:0)
关于代码的几点:
invokeAndWait
调用启动线程毫无意义。它没有害处,但却是一种浪费。仅使用该方法执行与UI相关的操作。Connector.open
的参数。根据{{3}},具有{protocol}://[{host}]:[{port}]
形式的参数将在客户端模式下打开连接(这是有意义的,因为您在接收部分),而不包括主机部分将在服务器中打开它模式。答案 1 :(得分:0)
您引用的错误是抱怨使用带有字符串参数的String构造函数。由于字符串在Java-ME中是不可变的,这只是浪费。您可以直接使用参数字符串:
调用可疑方法:java.lang.String。(String)位于:mypackage.MyApp $ ListeningThread.run()
//String address = new String(d.getAddress());
String address = d.getAddress();
// getData() returns a byte[], so this is a different constructor
// However, this leaves the character encoding unspecified, so it
// will default to cp1252, which may not be what you want
String msg = new String(d.getData());