如何在黑莓设备中以编程方式发送短信

3

如何在黑莓设备上以编程的方式发送短信?

我看到有人说,我需要服务器端和客户端代码才能发送短信。这是真的吗?为了从一个设备向另一个设备或从仿真器向设备发送消息,我是否真的需要服务器端和客户端代码?

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

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

在我的代码中,我遇到了运行时异常。请帮助我。谢谢。 - Riddhi Barbhaya
@Riddhi-我有一个类似的问题...我可以将应用程序运行到完成,但是我在BlackBerry模拟器的'Messages'文件夹中没有看到我的消息!你能帮我解决吗? - Name is Nilay
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();
}

@Signare-我有一个类似的问题...我可以运行应用程序直到完成(我会得到“消息发送成功:TextMessage”的对话框),但我在BlackBerry模拟器的“消息”文件夹中看不到我的消息!你能帮我解决这个问题吗? - Name is Nilay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接