一种是加messagelister来实现.一开始我就用的这种,不过这种方法在获得短信的incoming时有时会没有反映,倒是BB DEV有人建议用第二种方法.
MessageConnection messconn;
messconn = (MessageConnection) Connector.open("sms://:0");
MessageListener ml = new MessageListener(){
public void notifyIncomingMessage(MessageConnection conn) {}
messconn.setMessageListener(ml);
第2种是用johnbibs给我的.之前我也知道,但一直没用过,因为感觉整个代码就像
while (true){
do sth.
}
在做无线循环,我怕会造成CPU负担和电力过多的消耗如shangmail的伪pushmail.
// LISTEN FOR INCOMING MESSAGES
Datagram d = null;
try {
d = dc.newDatagram(dc.getMaximumLength());
dc.receive(d);
byte[] bytes = new byte[d.getLength()];
bytes = d.getData();
String msg = new String(bytes);
String address = d.getAddress();
int smsSize = d.getLength();
if (msg != null || msg.trim().equalsIgnoreCase("")) {
System.out.prinln(msg);
}
} catch (IOException ioe) {
System.out.println();
} catch (Exception ex) {
System.out.println();
}