BlackBerry - 发送短信时出现例外情况

时间:2009-12-13 18:19:47

标签: networking blackberry sms blackberry-storm

此代码失败,向手机号码发送短信。它抛出一个异常消息:“事件分派线程上不允许阻塞操作”。

所以我创建了一个单独的线程来执行SMS代码,但我仍然在观察相同的异常。

我做错了什么?

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

编辑:2009年12月14日
我通过创建一个单独的线程然后不使用端口等解决了这个问题。

这是:

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

0 个答案:

没有答案