首页 » 黑莓开发 » 监听短信的两种方法

监听短信的两种方法

3821 0

一种是加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();
}

文章评分1次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2009/11/%e7%9b%91%e5%90%ac%e7%9f%ad%e4%bf%a1%e7%9a%84%e4%b8%a4%e7%a7%8d%e6%96%b9%e6%b3%95/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

    没有相关的文章

评论前先开启评论开关: