BlackBerry中的“发送消息”中的运行时异常

时间:2012-10-12 10:00:12

标签: blackberry sms blackberry-simulator blackberry-jde sms-gateway

当我运行此代码时,我得到RunTime Exception。如果您有任何想法,请通过它并帮助我。感谢..

private void sendSMS(String phone, String message) throws IOException 
    {

//       TODO Auto-generated method stub
        Dialog.alert("Hello..In Send SMS Function");
        System.out.println("in send sms function");

        MessageConnection conn =
            (MessageConnection)Connector.open("sms://+919099956325");
        TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
        tmsg.setAddress("sms://+919429441335");
        tmsg.setPayloadText("HIIiii");
        System.out.println("Text message is>>"+tmsg);
        conn.send(tmsg);
}

1 个答案:

答案 0 :(得分:1)

而不是     System.out.println("Text message is>>"+tmsg);

使用

System.out.println("Text message is>>"+tmsg.getPayloadText());

同样Connector.open是一个阻塞操作,不应该从主事件线程调用。

你有Dialog.alert,它只适用于事件线程。这样做

UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                Dialog.alert("Hello..In Send SMS Function");
                }
            });

试试这段代码。这将启动一个新线程并调用sentms方法

new Thread(new Runnable() {

        public void run() {
             try {
                sendSMS("123456789","message");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }).start();

          private void sendSMS(String phone, String message) throws IOException 
          {
    try {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
            Dialog.alert("Hello..In Send SMS Function");
            }
        });
        System.out.println("in send sms function");

        MessageConnection conn =
            (MessageConnection)Connector.open("sms://+919099956325");
        TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
        tmsg.setAddress("sms://+919429441335");
        tmsg.setPayloadText("HIIiii");
        System.out.println("Text message is>>"+tmsg.getPayloadText());
        conn.send(tmsg);
    } catch (Exception e) {
        System.out.println("Exception is >>"+e.toString());
    }
}