Blackberry - Exception when sending SMS programmatically

Posted by user213199 on Stack Overflow See other posts from Stack Overflow or by user213199
Published on 2009-12-13T18:19:46Z Indexed on 2010/03/19 12:21 UTC
Read the original article Hit count: 644

Hello all,

I am developing a Blackberry application. I am trying to send SMS programmatically to GSM number. I have gone through many queries and answers related to that and finally added the code for that as below. When the code tries to execute sending some text message to the particular mobile number, it doesn't send and throws exception as "blocking operation not permitted on event dispatch thread". So i created a separate background thread where i put the SMS code and running the code now. But still observing the same exception.

Could someone please suggest what am i doing wrong (or) how to do that?

class DummyFirst extends MainScreen {
    private Bitmap background;
    private VerticalFieldManager _container;
    private VerticalFieldManager mainVerticalManager;
    private HorizontalFieldManager horizontalFldManager;
    private BackGroundThread _thread;
    CustomControl buttonControl1;

    public DummyFirst() {
    	super();
    	LabelField appTitle = new LabelField("Dummy App");
    	setTitle(appTitle);
    	background = Bitmap.getBitmapResource("HomeBack.png");
    	_container = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL
    			| Manager.NO_VERTICAL_SCROLLBAR) {
    		protected void paint(Graphics g) {
    			// Instead of these next two lines, draw your bitmap
    			int y = DummyFirst.this.getMainManager()
    			    	.getVerticalScroll();
    			g.clear();
    			g.drawBitmap(0, 0, background.getWidth(), background
    					.getHeight(), background, 0, 0);
    			super.paint(g);
    		}
    		protected void sublayout(int maxWidth, int maxHeight) {
    			int width = background.getWidth();
    			int height = background.getHeight();
    			super.sublayout(width, height);
    			setExtent(width, height);
    		}
    	};
    	mainVerticalManager = new VerticalFieldManager(
    			Manager.NO_VERTICAL_SCROLL | 
    			Manager.NO_VERTICAL_SCROLLBAR) {
    		protected void sublayout(int maxWidth, int maxHeight) {
    			int width = background.getWidth();
    			int height = background.getHeight();
    			super.sublayout(width, height);
    			setExtent(width, height);
    		}
    	};
    	HorizontalFieldManager horizontalFldManager = 
    	    		new HorizontalFieldManager(Manager.USE_ALL_WIDTH);
    	buttonControl1 = new CustomControl("Send", ButtonField.CONSUME_CLICK,
    			83, 15);
    	horizontalFldManager.add(buttonControl1);
    	this.setStatus(horizontalFldManager);
    	FieldListener listner = new FieldListener();
    	buttonControl1.setChangeListener(listner);
    	_container.add(mainVerticalManager);
    	this.add(_container);
    }

    class FieldListener implements FieldChangeListener {
    	public void fieldChanged(Field f, int context) {
    		if (f == buttonControl1) {
    			_thread = new BackGroundThread();
    			_thread.start();
    		}
    	}
    }

    private class BackGroundThread extends Thread {
    	public BackGroundThread() {
    		/*** initialize parameters in constructor *****/
    	}
    	public void run() {
    		// UiApplication.getUiApplication().invokeLater(new Runnable()
    		UiApplication.getUiApplication().invokeLater(new Runnable() {
    			public void run() {
    				try {
    					MessageConnection msgConn = 
    	    		    		    (MessageConnection) Connector
    					        .open("sms://:0");
    					Message msg = msgConn
    					    .newMessage(
    					        MessageConnection.TEXT_MESSAGE);
    					TextMessage txtMsg = (TextMessage) msg;
    					String msgAdr = "sms://+919861348735";
    					txtMsg.setAddress(msgAdr);
    					txtMsg.setPayloadText("Test Message");
    	    		    		// here exception is thrown
    					msgConn.send(txtMsg);
    					System.out.println("Sending" + 
    					    " SMS success !!!");
    				} catch (Exception e) {
    					System.out.println(e.getMessage());
    					e.printStackTrace();
    				}
    			} // run
    		});
    	}
    }

    public boolean onClose() {
    	System.out.println("close event called, request to be" + 
    	    " in the backgroud....");
    	UiApplication.getUiApplication().requestBackground();
    	return true;
    }
}

I resolved this issue by creating a separate thread and then not using Port etc.

Here it is:

SMSThread smsthread = new SMSThread("Some message",mobNumber);
smsthread.start();

class SMSThread extends Thread {
Thread myThread; MessageConnection msgConn; String message; String mobilenumber;
public SMSThread( String textMsg, String mobileNumber ) {
message = textMsg;

    mobilenumber = mobileNumber;
}
public void run() {
    try {
        msgConn = (MessageConnection) Connector.open("sms://+"+ mobilenumber);
        TextMessage text = (TextMessage) msgConn.newMessage(MessageConnection.TEXT_MESSAGE);
        text.setPayloadText(message);
        msgConn.send(text);
        msgConn.close();
    } catch (Exception e) { 
         System.out.println("Exception: " + e);
    }
}

}

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about blackberry-storm