3

如何在 BlackBerry 中以编程方式发送 SMS?

我在某处读到,我需要服务器端和客户端代码来发送 SMS。这是真的吗?对于从一台设备向另一台设备或从模拟器向设备发送消息,我真的需要服务器端和客户端代码吗?

我在客户端的某个地方找到了这段代码,但我没有得到输出。

private void sendSMS(String phone, String message) throws RuntimeException, ServicesManagerException, IOException 
{
    // TODO Auto-generated method stub

    System.out.println("in send sms function");
    MessageConnection conn =
        (MessageConnection)Connector.open("sms://+919099087960");
    BinaryMessage msgOut = (BinaryMessage) conn.newMessage(MessageConnection.BINARY_MESSAGE);
    msgOut.setPayloadData("my binary payload".getBytes("UTF-8"));
    conn.send(msgOut);
}
4

1 回答 1

5

您不需要任何服务器端代码。检查以下代码。

static String msg="hai";
try {
    new Thread() {
        public void run() {
            if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
                DatagramConnection dc = null;
                try {
                    dc = (DatagramConnection) Connector.open("sms://+919099087960");
                    byte[] data = msg.getBytes();
                    Datagram dg = dc.newDatagram(dc.getMaximumLength());
                    dg.setData(data, 0, data.length);
                    dc.send(dg);
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Message Sent Successfully : Datagram");
                                Dialog.alert("Message Sent Successfully");
                            } catch (Exception e) {
                                System.out.println("Exception  : " + e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println("Exception  : " + e.toString());
                    e.printStackTrace();
                } finally {
                    try {
                        dc.close();
                        dc = null;
                    } catch (IOException e) {
                        System.out.println("Exception  : " + e.toString());
                        e.printStackTrace();
                    }
                }
            } else {
                MessageConnection conn = null;
                try {
                    conn = (MessageConnection) Connector.open("sms://+919099087960");
                    //generate a new text message
                    TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                    //set the message text and the address
                    tmsg.setAddress("sms://+919099087960");
                    tmsg.setPayloadText(msg);
                    //finally send our message
                    conn.send(tmsg);
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            try {
                                System.out.println("Message Sent Successfully : TextMessage");
                                Dialog.alert("Message Sent Successfully : TextMessage");
                            } catch (Exception e) {
                                System.out.println("Exception  : " + e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    System.out.println("Exception  : " + e.toString());
                    e.printStackTrace();
                } finally {
                    try {
                        conn.close();
                        conn = null;
                    } catch (IOException e) {
                        System.out.println("Exception  : " + e.toString());
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();
} catch (Exception e) {
    System.out.println("Exception  : " + e.toString());
    e.printStackTrace();
}
于 2012-10-09T13:46:27.050 回答