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
Regular Contributor
Posts: 63
Registered: ‎07-01-2010
My Device: BlackBerry Curve 8900
My Carrier: Orange

Why can not send SMS on main event thread?

 

Hi,

 

I was using this code to send an SMS

 

try {
                    MessageConnection mc = (MessageConnection)Connector.open("sms://" + phoneNumber);
                    TextMessage m = (TextMessage) mc.newMessage(MessageConnection.TEXT_MESSAGE);
                    m.setPayloadText("Testing");
                    mc.send(m);
                    mc.close();
                } catch (IOException e) {

....}

 

as recommended in the KB article.

 

When the user clicks a 'send SMS' button in my application. The SMS was not sent and when I debugged the code on a live device it never returned from the call mc.send(m). I put this in a separate thread and it now works perfectly.

 

I know I have a solution but I don't understand so can someone please take the time to explain to me why this did not return from send when running on the main event thread?

 

I know RIM recommend opening some connections on a separate thread as they perform blocking operations and will lock your application but I don't actually want the user to be able to do anything else until the SMS has been sent so I am happy for the sreen to 'freeze'.

 

 

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Why can not send SMS on main event thread?

You say 'never returned'.  That surprises me.  Did it throw an exception that you never detected?  Put a try catch around the send to find out out.  Whatever exception you see will tell you why you shouldn't do this on the event Thread. 

Regular Contributor
Posts: 63
Registered: ‎07-01-2010
My Device: BlackBerry Curve 8900
My Carrier: Orange

Re: Why can not send SMS on main event thread?

 

Hi Peter,

 

Thanks for all your help today. I managed to send SMS using the simulator rather than a live device which made it easier for debugging. When I tried to run my original code ie not on a separate thread, when not debugging I get the screen showing uncaught RuntimeException. When I debug it in the simulator the debugger also shows me it throws a runtime exception and when I catch Exception and get the details it's the runtime exception 'sms send blocking operation can not be run on the main event thread'. The same conclusion I'd come to but I might have got there quicker if I'd caught all exceptions Smiley Wink

 

However, I've double checked just incase I'm doing something wrong but when I run it on the actual device if I don't catch all exceptions, debugging or not it definitely appears as if the application just hangs. The exception is definitely being thrown because when I add the catch it does catch it, but if I don't have an explicit catch it appears as if the exception isn't caught and my application just hangs. I didn't think you needed to catch runtime exceptions? I expected it to behave on the device as it does on the simulator and give me the white screen with uncaught runtime exception?

 

 

Still doesn't explain why. Have you any idea? Like I said before if it's simply blocking I'd be quite happy for my application to be blocked. Is sms send doing something that requires multi-threads? Waiting for a response or something? So it actually locks if there's only one thread running? I find the use of the word 'blocking operation' a little ambiguous.

 

Thanks again.

 

Now to solve my email problems.. Smiley Happy

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Why can not send SMS on main event thread?

"I expected it to behave on the device as it does on the simulator and give me the white screen with uncaught runtime exception? "

 

I would have expected this behavior too.  I can't explain why it does not.  I have no idea why you are seeing what you are seeing when running on the Event Thread.  But I'm not sure this is an area worth investigating, since you really shouldn't be doing it anyway. 

 

You say: " if it's simply blocking I'd be quite happy for my application to be blocked"

 

Unfortunately you are not given that choice.  The Blackberry OS tries very hard to stay responsive, so checks the Event Thread.  Until the design changes from this, we are stuck.  So if we want to 'block' the user, we have to do something like put a up a blocking popup screen until the operation which is blocking (but running in a separate Thread) is finished.