Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
prabhu_mp_99
Posts: 93
Registered: ‎08-17-2009
My Device: Not Specified
Accepted Solution

Issue with sending SMS programmatically for multiple recipients:

Hi Everyone,

 

I am trying to send some info via SMS programmatically for multiple recipients at a time. When i am trying to send SMS to one recipient, it is working fine. I am able to send the data to one recipient successfully without any issues. But when i am trying to send to multiple(more than one) recipients as code below in "for" loop, it is not sending to all the recipients. i.e, sometimes it sent the data to only one recipient though there are two recipient numbers, (or) sometimes it doesn't even send SMS to anyone (or) sometimes it sent to all the recipients. It is not consistent like that way when trying to send to multiple recipients at a time.

I even tried to use "invokeAndWait" before calling SMSThread code, but still the same problems.

 

Could someone guide how can i resolve for sending a content to multiple users at a time?

 

public void SendMultipleSMS()
    {                           
        // multiple recipients stored here    
        _data = (Vector) store.getContents();
        long totalSize = _data.size(); // totalSize - recipients count
        boolean yesLastSMS = false;
        
        // totalSize - recipients count
        if ( totalSize>0 )
        {
            _payload = null;
            _payload = contactsDetToSend(); // Content to send
            
            for ( int i=0; i<totalSize; i++ )
            {                       
                if ( _payload!=null )
                {                                               
                 //   UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                 //   public void run() {
                        
                            
                            //Vector data = (Vector) store.getContents();
                            //long size = data.size(); 
                            
                            if ( i==(totalSize-1) )
                                yesLastSMS = true;
                            
                            String[] items = new String[(int)totalSize];
                            
                            items[i] = GetStoredContactPhNumber(i);
                            System.out.println( "Recipient number: " + items[i] );
                            
		            // Thread to send SMS
                            SendSMS sendSMS = new SendSMS(items[i],_payload, yesLastSMS);
                            sendSMS.start();
                        
                  //      }
                  //  });
                }                                  
            }
        }
        else
            Dialog.inform( "Recipient contact list is empty.");
                
    }

    
    class SendSMS extends Thread
    {
        boolean yeslastsms = false;
        private String _payloadText;
        
        public SendSMS(String address, String payloadText, boolean bLastSMS)
        {
            _address = address;
            yeslastsms = bLastSMS;
            _payloadText = payloadText;
            System.out.println("***********" + _address + " " + _payloadText);
        }
        
        public void run()
        {
            try
            {
                MessageConnection _mc = (MessageConnection) Connector.open("sms://" + _address);
                TextMessage _tm = (TextMessage) _mc.newMessage(MessageConnection.TEXT_MESSAGE);
                _tm.setPayloadText(_payloadText);
                _mc.send(_tm);
                if(null != _mc)
                    _mc.close();
                
                if ( yeslastsms )
                {    
                    CloseSendScreen css = new CloseSendScreen();
                    _app.invokeLater(css);
                }
            }
            catch(Exception e)
            {
                System.out.println("*********** Exception in _forwardButton");
                
                System.out.println("Exception: e" + e);
            }
        }
    }

 

Please use plain text.
Developer
prabhu_mp_99
Posts: 93
Registered: ‎08-17-2009
My Device: Not Specified

Re: Issue with sending SMS programmatically for multiple recipients:

I fixed it by moving recipients "for" loop code inside the SMSThread itself and sending to multile numbers at once.

Please use plain text.