how to send the text message to multiple numbers i

2019-08-21 11:31发布

问题:

This question already has an answer here:

  • Send multiple SMS on BlackBerry 2 answers

Send the Text message(SMS) to multiple numbers in blackberry java applications. i tried the program to send the text message with single mobile numbers. how to i do in multiple mobile numbers?

This code is i'm using to send the message to multiple mobile number. Its shows the log as 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:

sending SMS programmatically for multiple recipients: Link