title: How to "properly" send sms when call is received?
body: <p>I am trying to have an Android <strong>Service</strong> listen for an incoming phone call and when one does occur, grab the incoming number and text it a message.</p>
  
  <p>In my service I have made a <strong>PhoneStateListener:</strong></p>
  
  <pre><code>TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  PhoneStateListener listener = new PhoneStateListener()
  {
      @Override
      public void onCallStateChanged(int state, String incomingNumber)
      {
          switch(state)
          {
              case TelephonyManager.CALL_STATE_IDLE:
                  Log.d(TAG, "Phone: Idle");
              break;
              case TelephonyManager.CALL_STATE_RINGING:
                  Log.d(TAG, "Phone: Ringing.");
                  Log.i(TAG, "Incoming call from: " + incomingNumber);
                  sendSms();
              break;
              case TelephonyManager.CALL_STATE_OFFHOOK:
                  Log.d(TAG, "Phone: Off Hook");
              break;
          }
      }
  };
  tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
  </code></pre>
  
  <p>My <strong>sendSms()</strong> function is as follows:</p>
  
  <pre><code>private void sendSms()
  {
      SmsManager manager = SmsManager.getDefault();
  
      wakeLock.acquire();
      Log.d(TAG, "Wake Lock Acquired!");
  
      if (getMessageContent(getInformStatus()).length() &gt; 160)
      {
          ArrayList&lt;String&gt; messagelist = manager.divideMessage(getMessageContent(getInformStatus()));
  
          manager.sendMultipartTextMessage(getReturnAddress(), null, messagelist, null, null);
          Log.i(TAG, "Multipart Text Message Sent!");
      }
      else
      {
          manager.sendTextMessage(getReturnAddress(), null, getMessageContent(getInformStatus()), sentPI, null);
          Log.i(TAG, "Text Message Sent!");
      }
  
      wakeLock.release();
      Log.d(TAG, "Wake Lock Released!");
  }
  </code></pre>
  
  <p>I even have an <strong>SMS_SENT</strong> Broadcast Receiver to check if the text message sent out correctly that will recall the sendSms() function if it did not sent for whatever reason:</p>
  
  <pre><code>resend = new BroadcastReceiver()
  {
      @Override
      public void onReceive(Context context, Intent intent) 
      {
          switch (getResultCode())
          {
              case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                  Log.e(TAG, "Text did NOT send (GENERIC_FAILURE)");
                  Log.i(TAG, "Attempting to resend");
                  sendSms();
              break;
              case SmsManager.RESULT_ERROR_NO_SERVICE:
                  Log.e(TAG, "Text did NOT send (NO_SERVICE)");
                  Log.i(TAG, "Attempting to resend");
                  sendSms();
              break;
              case SmsManager.RESULT_ERROR_RADIO_OFF:
                  Log.e(TAG, "Text did NOT send (RADIO_OFF)");
                  Log.i(TAG, "Attempting to resend");
                  sendSms();
              break;
          }
      }
  };
  registerReceiver(resend, new IntentFilter("android.provider.Telephony.SMS_SENT"));
  </code></pre>
  
  <p>This works on my phone fine, however on other people's phones it likes to sometimes toggle back and forth between <strong>RINGING</strong> and <strong>IDLE</strong> in the PhoneStateListener a few times before "sending"..  The phone actually says that the application is trying to <strong>send multiple text messages</strong> and asks the user if that is okay.  When they click yes, it just asks again.</p>
  
  <p>To make it better, it <em>never</em> actually sends the text message either..</p>
  
  <p>On another phone, it doesn't do this at all..  The other phone will go through the motions and the <strong>logcat</strong> is identical to mine.. It actually says it sends, but the recipient never <em>gets</em> the SMS..</p>
  
  <p>It is suppose to be an auto-response service of sorts and I wanted to try and make it work on at least Froyo, Gingerbread, and Ice Cream Sandwich..</p>
  
  <p>By the way, it works 100% on my:<br />
  &nbsp;&nbsp;Galaxy Nexus (toro) (Android 4.0.3)<br />
  &nbsp;&nbsp;Droid 1 (sholes) (Android 2.3.7)<br />
  &nbsp;&nbsp;HTC Thunderbolt (mecha) (Android 2.3.7)<br />
  <br />
  However, if it's my friend's:<br />
  &nbsp;&nbsp;Nexus S 4G (crespo4g) (Android 4.0.3)<br />
  &nbsp;&nbsp;&nbsp;&nbsp;or my other friend's<br />
  &nbsp;&nbsp;Galaxy Nexus (toro) (Android 4.0.3)<br />
  <br />
  It does <strong>not</strong> work..<br /></p>
  
  <p>What I really don't understand is that it doesn't work with my friend with the exact same phone as me..</p>
  
  <p>I feel like this code implementation is a hit or miss and was wondering if anyone had some <strong>insight</strong> to help out.</p>
  
  <p>Appreciate it!</p>