Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 34
Registered: ‎12-04-2008
My Device: Not Specified
Accepted Solution

SMS Listener for outgoing SMS throws IOException immediately after open call

I had previously created a class to send SMS messages that had been working perfectly until I added the listener. When the listener is added as soon as I start the thread it throws an IOException what am I doing wrong ? The only thing I can think of is some sort of thread problem, please help.

 

Here is the SMS & Listener Class:

 

 

private static final class SmsMessage
{
private String _address;
private String _msg;

public SmsMessage(String address, String msg)
{
_address = address;
_msg = msg;
}

public Message toMessage(MessageConnection mc)
{
TextMessage m = (TextMessage)mc.newMessage(MessageConnection.TEXT_MESSAGE,"sms://" + _address);
m.setPayloadText(_msg);
return m;
}
} // SmsMessage

private final class SendThread extends Thread
{
private static final int TIMEOUT = 500; // ms
private Vector _msgs = new Vector(10); // Queue of 10
private volatile boolean _start = false;

// queue requests
public synchronized void send(String address, String msg)
{
_start = true;
_msgs.addElement(new SmsMessage(address, msg));
}

public synchronized void stop()
{
_stop = true;
try {
if ( _mc != null ){
_mc.close();
}
} // catch (IOException e ) {
catch (Throwable t) {
t.printStackTrace();
System.exit(0);
}

}

public void run()
{
try {
//closed by the stop() method
_mc = (MessageConnection)Connector.open("sms://");

if (_mc != null)
{
// set a listener to trap outgoing sms messages
_mc.setMessageListener(new OutboundSMSListener());
}

for(;;)
{
while( !_start && !_stop)
{
try {
sleep(TIMEOUT);
} // catch (InterruptedException e) {
catch (Throwable t) {
t.printStackTrace();
System.exit(0);
}
}
if ( _stop )
{
return;
}

while(true)
{
try {
SmsMessage sms = null;
synchronized (this)
{
if ( !_msgs.isEmpty() )
{
sms = (SmsMessage)_msgs.firstElement();
// take it out of the queue once we sent it
_msgs.removeElement(sms);
}
else
{
_start = false;
break;
}
}

_mc.send(sms.toMessage(_mc));

} // catch (IOException e) {
catch (Throwable t) {
t.printStackTrace();
System.exit(0);
}
}
}
} // catch (IOException e)
catch (Throwable t) {
t.printStackTrace();
System.exit(0);
}
}
} // SendThread

private static final class OutboundSMSListener implements OutboundMessageListener
{
public void notifyIncomingMessage(MessageConnection messageconnection)
{
// we don't care about incoming sms messages right now
}

public void notifyOutgoingMessage(Message message)
{
Dialog.alert("Detected An Outgoing SMS Event");
}
}

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: SMS Listener for outgoing SMS throws IOException immediately after open call

[ Edited ]

I think you need to specify the port number on your Connector.open request.

 

Something like "sms://:0"

 

You've got one big try there, so I don't know how you can tell where the exception is being thrown. I would also break this up. Don't put the open in your thread loop - there is no way for you to recover from a failure this way. Put the open in a separate method in your SMS message class, and allow it to throw the IOException on open from there.

 

Message Edited by RexDoug on 12-04-2008 01:25 PM
Developer
Posts: 34
Registered: ‎12-04-2008
My Device: Not Specified

Re: SMS Listener for outgoing SMS throws IOException immediately after open call

That got me passed the IOException but I am not able to see my debug dialog when I send an SMS. Do you think it is because I am trying to show the dialog from inside a thread or my listener class is going out of scope or something? Also is there a similar class to detect MMS messages being sent from the device ?
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: SMS Listener for outgoing SMS throws IOException immediately after open call

You cannot display a dialog from this listener thread. You need to get the dialog out of this thread and into you event thread.

 

Create a Runnable to display the dialog and use UiApplication.invokeLater()

Developer
Posts: 34
Registered: ‎12-04-2008
My Device: Not Specified

Re: SMS Listener for outgoing SMS throws IOException immediately after open call

Thanks. Makes sense.