java - 如何在黑莓java应用程序中向多个号码发送短信?

标签 java text blackberry sms blackberry-jde

将短信 (SMS) 发送到黑莓 Java 应用程序中的多个号码。我尝试使用该程序使用单个手机号码发送短信。我该如何操作多个手机号码?

我使用此代码将消息发送到多个手机号码。其将日志显示为 NullPointer Exception。

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.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
    public final class MyScreen extends MainScreen
    {
        String[] numbers={"number1","number2","number3"};
        public MyScreen()
        {        
            // Set the displayed title of the screen       
            setTitle("Sending SMS");
            LabelField ph_no_lbl= new LabelField("Enter the Phone Number");
            LabelField msg_lbl= new LabelField("Enter the Message");
            LabelField f_numbers= new LabelField();
            final EditField phone_number_edit= new EditField(Field.EDITABLE|FOCUSABLE);
            final EditField message_edit= new EditField(Field.EDITABLE|FOCUSABLE);
            ButtonField send = new ButtonField(Field.FOCUSABLE);
            send.setLabel("Click Here to Send");
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < numbers.length; i++) {
               result.append( numbers[i]+"\n" );
               //result.append( optional separator );
            }
            String mynewstring = result.toString();;
            phone_number_edit.setText(mynewstring);
            add(ph_no_lbl);
            add(phone_number_edit);
            add(msg_lbl);
            add(message_edit);
            add(send);
            send.setChangeListener(new FieldChangeListener() {

            public void fieldChanged(Field field, int context) {
                // TODO Auto-generated method stub
                final String getNumber= phone_number_edit.getText().toString();
                final String getMessage= message_edit.getText().toString();
                if(getNumber.length()==0 || getNumber.length()<=9 || getMessage.length()==0)
                {
                    Dialog.alert("Enter the Fields CareFully");
                }
                else
                {
                sendSMS(getNumber, getMessage);
                }
            }
        });
    }
//<<<<<<<Method Two>>>>>>>>>>>>>>>>>>>>
public static void sendSMS(final String no, final String msg) {
    // try {
    new Thread() {
        public void run() {

            boolean smsSuccess = false;
            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);
                    // / send successfully
                    smsSuccess = true;
                } catch (Exception e) {
                    System.out.println("Exception 1 : " + e.toString());
                    e.printStackTrace();
                    smsSuccess = false;
                } 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);
                    TextMessage tmsg = (TextMessage) conn
                            .newMessage(MessageConnection.TEXT_MESSAGE);
                    tmsg.setAddress("sms://" + no);
                    tmsg.setPayloadText(msg);
                    conn.send(tmsg);
                    smsSuccess = true;
                } catch (Exception e) {
                    smsSuccess = false;
                    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();
                    }
                }
            }
            if(smsSuccess)
            {
                UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        Dialog.alert("Message Sending Succcessfully :-)");
                    }
                }); 
            }else
            {
                UiApplication.getUiApplication().invokeLater(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        Dialog.alert("Message Sending Failure :-(");
                    }
                }); 

            }

        }
    }.start();
} 

}

最佳答案

以编程方式向多个收件人发送短信:Link

关于java - 如何在黑莓java应用程序中向多个号码发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15680840/

相关文章:

xml - 在 Blackberry 中创建 XML 的更好方法

html - 如何解决文本的响应问题

javascript - 读取文本文件

Blackberry dev - 关于 jre 1.5 的问题

java - SWING 中的代码/文本折叠

java - JPA 在表达式 : what is collection size limit? 中

java - 从 eclipse 构建路径中删除 Junit

java - 使用 Notepad++ 清理文本

java - 如何在 Hashmap<Hashmap<ArrayList>> 中存储位置索引和文档 ID

java - 将文件上传到 HttpConnection - BLACKBERRY/JavaME