in blackberry you can attach message listener to specific port.
try {
final MessageConnection conn = (MessageConnection) Connector.open("sms://:"+port);
conn.setMessageListener(new MessageListener() {
public void notifyIncomingMessage(MessageConnection mc) {
Message msg;
try {
msg = conn.receive();
} catch (InterruptedIOException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
String senderAddress = msg.getAddress(); // Get info from message
if (msg instanceof TextMessage) {
String msgReceived = ((TextMessage) msg).getPayloadText();
// Do something with the message here
} else if (msg instanceof BinaryMessage) {
byte[] msgReceived = ((BinaryMessage) msg).getPayloadData();
// do something with the binary message here
}
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
port=0 means you can listen for all default incoming sms.
if you attach the message listener to port other than 0, message will not appear in inbox.
but if you failed to handle this message, it will appear in inbox.
some restrictions are there for message listeners.
- you can't read SMS directly from inbox folder.
- only one 3rd party application can listen on one port.
e.g. if your application is listening on port 0, no other application can listen on this port.
- after blackberry restart, blackberry will remove you message listener.