我正在尝试使用BlackBerry 4.5 OS发送短信。
该send-SMS代码在8300 Curve上有效,但在我尝试过的任何其他设备上均无效。为什么?

以下是我发送短信的代码:

new Thread(){
    MessageConnection conn;
    public void run() {

    if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
        DatagramConnection dc = null;
        try {
            dc = (DatagramConnection) Connector.open(
                     "sms://"+ recepient.getRecepient());
            byte[] data = txt.getBytes();
            Datagram dg = dc.newDatagram(dc.getMaximumLength());
            dg.setData(data, 0, data.length);
            dc.send(dg);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dc.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    } else {
        try{
            conn = (MessageConnection) Connector.open("sms://"+ recepient.getRecepient());
            //generate a new text message
            TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
            //set the message text and the address
            tmsg.setAddress("sms://" + recepient.getRecepient());
            tmsg.setPayloadText(txt);
            //finally send our message
            conn.send(tmsg);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
}.start();

最佳答案

尝试使用此代码,我已经在Blackberry OS 5.0和6.0上测试了此代码。

package mypackage;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class HelloBlackBerryScreen extends MainScreen {

private BasicEditField basicEditField1, basicEditField2;
private String addr = "9998887777";
private String message = "This is a test message.";

public HelloBlackBerryScreen() {
    super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
    setTitle("HelloBlackBerry");

    basicEditField1 = new BasicEditField("To : ", addr, 500, BasicEditField.EDITABLE);
    basicEditField2 = new BasicEditField("Message : ", message, 500, BasicEditField.EDITABLE);

    ButtonField buttonField_1 = new ButtonField("Send", ButtonField.CONSUME_CLICK | ButtonField.FIELD_HCENTER);

    buttonField_1.setChangeListener(new FieldChangeListener() {
        public void fieldChanged(Field arg0, int arg1) {
            try {
                addr = basicEditField1.getText();
                message = basicEditField2.getText();
                sendSMS(addr, message);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Error send msg : " + e.toString());
            }
        }
    });

    this.add(basicEditField1);
    this.add(basicEditField2);
    this.add(buttonField_1);
}

private void sendSMS(final String no, final String msg) {
    try {
        new Thread() {
            public void run() {
                if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
                    DatagramConnection dc = null;
                    try {
                        dc = (DatagramConnection) Connector.open("sms://" + no);
                        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 : Datagram");
                                } catch (Exception e) {
                                    System.out.println("Exception **1 : " + e.toString());
                                    e.printStackTrace();
                                }
                            }
                        });
                    } catch (Exception e) {
                        System.out.println("Exception 1 : " + e.toString());
                        e.printStackTrace();
                    } finally {
                        try {
                            dc.close();
                            dc = null;
                        } catch (IOException e) {
                            System.out.println("Exception 2 : " + e.toString());
                            e.printStackTrace();
                        }
                    }
                } else {
                    MessageConnection conn = null;
                    try {
                        conn = (MessageConnection) Connector.open("sms://" + no);
                        //generate a new text message
                        TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
                        //set the message text and the address
                        tmsg.setAddress("sms://" + no);
                        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 **1 : " + e.toString());
                                    e.printStackTrace();
                                }
                            }
                        });
                    } catch (Exception e) {
                        System.out.println("Exception 3 : " + e.toString());
                        e.printStackTrace();
                    } finally {
                        try {
                            conn.close();
                            conn = null;
                        } catch (IOException e) {
                            System.out.println("Exception 4 : " + e.toString());
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();
    } catch (Exception e) {
        System.out.println("Exception 5 : " + e.toString());
        e.printStackTrace();
    }
}

protected boolean keyDown(int keycode, int status) {
    if (Keypad.key(keycode) == Keypad.KEY_ESCAPE) {
        close();
        return true;
    }
    return super.keyDown(keycode, status);
}
}

10-08 16:13