Hi everyone.
I'm writing an application that will (hopefully =) read sms sent to a blackberry 8310 handset. Specifically I am reffering to blackberry system messages. For now, I'd be happy to recieve/read any messages. I have tried two approaches with neither seeming to work (no message displayed). I used the blackberry
knowledge base article as reference and
developers guide Receive an SMS message:
Communicator being the parent class the instantiated the thread.
updateLocationScreen simply does an EditField.insert(msg);
Code:
//EXECUTED IN CONSTRUCTOR
ListeningThread listen = new ListeningThread(this);
listen.start();
//END EXECUTED IN CONSTRUCTOR
private static class ListeningThread extends Thread {
Communicator com;
private boolean _stop = false;
private MessageConnection _mc;
public synchronized void stop() {
_stop = true;
try {
_mc.close(); // Close the connection so the thread returns.
} catch (IOException e) {
System.err.println(e.toString());
}
}
ListeningThread(Communicator comPass){
com = comPass;
}
public void run() {
try {
com.updateLocationScreen("\n\nListening for messages...\n");
_mc = (MessageConnection)Connector.open("sms://");
for(;;) {
if ( _stop ) {
return;
}
Message m = _mc.receive();
String address = m.getAddress();
String msg = null;
if ( m instanceof TextMessage ) {
TextMessage tm = (TextMessage) m;
msg = tm.getPayloadText();
}
com.updateLocationScreen("Message received: " + msg);
com.updateLocationScreen("From: " + address);
//System.exit(0);
}
}
catch (IOException e) {
System.err.println(e.toString());
}
}
}
AND (the version I'd most prefer to work as its independant of type of message)
Code:
//EXECUTED IN CONSTRUCTOR
ListeningThread listen = new ListeningThread(this);
listen.start();
//END EXECUTED IN CONSTRUCTOR
//New SMS handling class
private static class ListeningThread extends Thread {
private boolean _stop = false;
private DatagramConnection _dc;
Communicator com;
ListeningThread(Communicator comPass){
com = comPass;
}
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 {
com.updateLocationScreen("\n\nListening for messages...\n");
_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());
com.updateLocationScreen("Message received: " + msg);
com.updateLocationScreen("From: " + address);
//System.exit(0);
}
} catch (IOException e) {
//System.err.println(e.toString());
}
}
}
Screen output never displays any recieved message data, it does display 'Listening for messages', as expected.
Any ideas?