如何发送短信到多个号码在BlackBerry Java应用程序? [英] how to send the text message to multiple numbers in blackberry java applications?

查看:340
本文介绍了如何发送短信到多个号码在BlackBerry Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BlackBerry Java应用程序发送短信(SMS)到多个号码。我尝试过的节目单手机号码发送短信。如何在我多个手机号码,怎么办?

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?

这code是我使用的消息发送给多个手机号码。它显示了日志为空指针异常。

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

}

推荐答案

发送短信编程为多个收件人:<一href=\"http://supportforums.blackberry.com/t5/Java-Development/Issue-with-sending-SMS-programmatically-for-multiple-recipients/m-p/600756#M124853\"相对=nofollow>链接

sending SMS programmatically for multiple recipients: Link

这篇关于如何发送短信到多个号码在BlackBerry Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆