我有代码
DatagramConnection _dc =(DatagramConnection)Connector.open("sms://");
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d); //Receive the sms
byte[] bytes = d.getData();
String address = d.getAddress(); //The address of the sms is put on a string.
String msg = new String(bytes);
以上代码是否连续监听收到的短信,还是只听1个短信? 如果它只是收听1条短信,请您提供代码,以便持续收听短信。
答案 0 :(得分:1)
您的代码只能读取一条短信。如果您需要阅读每个发送的短信,您需要像the official knowledge base article中所发布的那样循环:
DatagramConnection _dc = (DatagramConnection)Connector.open("sms://");
for(;;) {
Datagram d = _dc.newDatagram(_dc.getMaximumLength());
_dc.receive(d);
byte[] bytes = d.getData();
String address = d.getAddress();
String msg = new String(bytes);
System.out.println( "Received SMS text from " + address + " : " + msg);
}
现在出现一个问题:BB OS是否以串行方式向收听应用程序发送短信?如果它(我想是这样,但从未测试过那种程度),那么您需要做的就是尽快将消息转发给消费者(否则您将在SMS处理期间占用连接侦听器线程)。 / p>