views:

664

answers:

1

This code fails, sending a text message to a mobile number. It throws an exception with message: "blocking operation not permitted on event dispatch thread".

So I created a separate thread to execute the SMS code, but I am still observing the same exception.

What am I doing wrong?

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;
    }
}

EDIT: Dec 14, 2009
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);
        }
    }
}
A: 

I used code given but I am getting "IOCancelledException" and "IOException". And IOCancelledException.getMessage() / IOException.getMessage() giving null string, it does not give error message. Please help me understaing reason.

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 (IOCancelledException ioce) {
            System.out.println("IOCancelledException: " + ioce.getMessage());
        } catch (IOException ioe) {
            System.out.println("IOException: " + ioe.getMessage());
        } catch (Exception e) { 
            System.out.println("Exception: " + e);
        }
    }
}
vikram deshpande
did u tried on the device as in my case it threw exception in simulator but not on device
SWATI