如何在黑莓java应用程序中将文本消息发送到多个数字?

时间:2013-03-28 11:41:21

标签: java text blackberry sms blackberry-jde

在黑莓java应用程序中将短信(SMS)发送给多个号码。我尝试用该程序发送带有单个手机号码的短信。我怎么做多个手机号码?

此代码用于将邮件发送到多个手机号码。它将日志显示为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();
} 

}

1 个答案:

答案 0 :(得分:1)

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