title: Sending text messages programmatically in android
body: <p>OK.  I am sending text messages through my app.  After a text message is sent, it sends a status update to a server.  This portion works okay, but the problem I am running into is twofold. I am not sure if they are related, but I assume that they are.</p>
  
  <p>My app has the ability to send a single text to multiple users.  Here is a sample of the code...</p>
  
  <pre><code>if(phoneNumbers.length &gt; 0 &amp;&amp; message.getText().toString().equals("") == false)
  {
      for(int i=0;i&lt;phoneNumbers.length;i++)
      {
          sms = SmsManager.getDefault();
          try
          {
              sms.sendTextMessage(phoneNumbers[i], null, message.getText().toString(), null, null);
              sentQuantity++;
          }
          catch(IllegalArgumentException e)
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Basically, it just loops through an array of phone numbers, and sends the text one at a time.  Here is where part of my issue lies.  If I choose 3 or more numbers to send the text to, sometimes not all of the texts actually get sent.  It happens very randomly.  </p>
  
  <p>I assume it is because there is a delay between sending each individual message, but the code doesn't wait long enough.  I reached this assumption because if I step into the program using eclipse and manually go through the app, everything always works just fine.</p>
  
  <p>My other issue is when I send off the text message status update to a web server.</p>
  
  <p>Immediately after the text messages get sent, the app then connects to the internet and tells the server via an http post the number of texts that were sent.  Here is my snippet of internet code...</p>
  
  <pre><code>for(int i = 0; i &lt; postNames.length; i++)
  {
      nameValuePairs.add(new BasicNameValuePair(postNames[i], postValues[i]));
      }
  
      //http post
      try{
  
              HttpParams httpParameters = new BasicHttpParams();
              int timeoutConnection = 10000;
  
              HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection );
  
              HttpClient httpclient = new DefaultHttpClient(httpParameters);              
              HttpPost httppost = new HttpPost(webAddress);
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
  </code></pre>
  
  <p>This section just compiles the items for the post, connects to a web page, and sends the post.  The key here is the 10 second connection timeout. Once again, like I said earlier, the internet connection happens immediately after sending the texts.  So, if I go into debug mode and manually step through the process, everything works fine.  But if I just run the app on my phone, I will get a connection time out error.</p>
  
  <p>Now, I am hoping that if I can reduce the number of text messages to one single text, regardless of the number of recipients, that would be awesome.  I have tried separating the phone numbers with a comma, and that didn't work.  I also tried separating the numbers with a semi-colon (exactly like how Microsoft Outlook, or GMail lets you add multiple recipients to an email).  None of those worked for me.  Does anyone have any suggestions? Even if there is a different approach altogether, that would be appreciated.  Oh, and I don't want to use the Google Messaging intent to send the messages, I need to use my own app.</p>