Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java J2ME Blackberry
How to send Text Messages in Blackberry?
i am trying this code but unable to send text messages:

<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 11-Sep-12 21:41pm
Comments
Joan Murt at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

try this below code for sending text messages :

<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>
  Permalink  

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

  Print Answers RSS
0 Sascha Lefévre 490
1 Sergey Alexandrovich Kryukov 413
2 Maciej Los 210
3 Abhinav S 205
4 Richard Deeming 170
0 Sergey Alexandrovich Kryukov 6,928
1 OriginalGriff 6,381
2 Maciej Los 2,782
3 Abhinav S 2,732
4 Peter Leow 2,694


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100