blackberry - 如何在黑莓中以编程方式发送短信

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

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

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

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

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

最佳答案

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

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

关于blackberry - 如何在黑莓中以编程方式发送短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12801267/

相关文章:

BlackBerry - 在自己的项目中使用自己的 JAR 文件

java - 使用 Java SE 功能的 BlackBerry 问题

iphone - 向多个号码发送短信(iPhone SDK)

android - 发送同一条短信两次

html - BlackBerry WebWorks 应用程序滚动

c++ - 如何在 BlackBerry 10 Simulator 上模拟捏合?

java - 网络 I/O 在 BlackBerry OS 5 上挂起

java - 使用java通过tomcat服务器发送短信

blackberry-simulator - 部署失败 : Info: Sending request: Install and Launch

java - Blackberry:如何在应用程序中嵌入 YouTube 视频