Click here to Skip to main content
15,886,038 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
How to send Text Messages in Blackberry?
i am trying this code but unable to send text messages:

Java
<pre lang="xml">Dialog.inform("SEND SMS BLOCK: \n phno:"+ph_no+"MESSAGE :"+ message );
        String addr = "sms://"+ph_no;
        Dialog.inform("<<<<<<<<<ADDR>>>>>>>>> : "+addr);
        MessageConnection conn = null;
        Dialog.inform("<<<<<<<<<CONN>>>>>>>>> : "+conn);
        try
        {
            conn = (MessageConnection) Connector.open("sms://");
            Dialog.inform("<<<<<<<<<CONN in try catch>>>>>>>>> : "+conn);
            //generate a new text message
            TextMessage msgOut = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
            Dialog.inform("<<<<<<<<<MSG OUT>>>>>>>>> : "+msgOut);
//          TextMessage txtMsg = (TextMessage) message;

            msgOut.setPayloadText(message);
            msgOut.setAddress("sms://"+ph_no);
            Dialog.inform("<<<<<<<<< Address ======== : "+msgOut.getAddress());
            Dialog.inform("======== MESSAGE >>>>>>>>> : "+msgOut.getPayloadText());
            Dialog.inform("<<<<<<<<<Message>>>>>>>>> : "+message);
            try
            {
                conn.send(msgOut);
            }
            catch (Exception e)
            {
                Dialog.inform("><><>><>><>><><>><><><>><><>< : " +  e.toString());
            }
        }
        catch(IOException e)
        {
            Dialog.inform("+++++++++++++++++++++"+e.toString());
            System.out.println("+++++++++++++++++++++"+e.toString());
        }
        finally
        {
            try
            {
                conn.close();
                conn = null;
            }
            catch(IOException e)
            {
                Dialog.inform("<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>"+e.toString());
                System.out.println("<<<<<<<<<<<<<<>>>>>>>>>>>>>>"+e.toString());
            }
        }
Posted
Comments
Joan M 12-Sep-12 4:29am    
As all your posts here, this is an incomplete question and only a code dump that we don't know what does, where it fails, the result it is giving you...

1 solution

try this below code for sending text messages :

Java
<small>public final class HelloBlackBerryScreen extends MainScreen
{
	private BasicEditField basicEditField1, basicEditField2;
	private String addr = "9888919119";
	private String message = "This is a test message.";
    public HelloBlackBerryScreen()
    {      
    	super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
        setTitle("Send SMS :");
        basicEditField1 = new BasicEditField("To : ", addr, 11, 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);
    }
}</small>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900