title: Android: Send/Receive SMS text messages when running in background
body: <p>My app used to work when it was running in the background. The app simply sends a text message to a person whenever it receives a text message (essentially an away message). </p>
  
  <p>With the new update they removed the permission BROADCAST_SMS which is what I think was allowing the app to operate in the background.</p>
  
  <p>So my problem is this, I need my app to work when it is not displayed. In other words, My app currently works as expected when it is open and focused but as soon as the user minimizes/sends the app to the background, the app no longer works.</p>
  
  <p>Here is the code behind my manifest.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.katianie.awayreply2"
      android:versionCode="2"
      android:versionName="2.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="21" /&gt;
  
      &lt;uses-permission android:name="android.permission.SEND_SMS"/&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS"/&gt;
      &lt;uses-permission android:name="android.permission.READ_SMS"/&gt;
      &lt;uses-permission android:name="android.permission.WRITE_SMS"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" 
          android:persistent="true" &gt;
          &lt;activity
              android:name="com.katianie.awayreply2.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Here is how I am sending a sms text message:</p>
  
  <pre><code>public void sendTextMessage(String phoneNo, String message)
  {
      //Used to detect when the sent text message was/was not delivered.
      String actionStr = "android.provider.Telephony.SMS_DELIVERED";
      Intent theDeliveredIntent = new Intent(actionStr);
      PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(myMainActivity, 0, theDeliveredIntent, 0);
      SmsManager smsManager = SmsManager.getDefault();
  
      //When the SMS has been delivered, Notify the user with a simple toast.
      myMainActivity.registerReceiver(new BroadcastReceiver() 
      {
          //Called when the away message has gotten to the destination.
          @Override
          public void onReceive(Context arg0, Intent arg1) 
          {
              int resultCode = getResultCode();
  
              if(resultCode == Activity.RESULT_OK)
              {
                  Toast.makeText(myMainActivity.getBaseContext(), "Away message delivered.", Toast.LENGTH_SHORT).show();
              }
              else if(resultCode == Activity.RESULT_CANCELED)
              {
                  Toast.makeText(myMainActivity.getBaseContext(), "Away message could NOT be delivered.", Toast.LENGTH_SHORT).show();
              }
  
              myHasSentMessage = false;
  
          }
      }, new IntentFilter(actionStr));
  
      //Send the text message
      if(!myHasSentMessage)
      {
          smsManager.sendTextMessage(phoneNo, null, message, deliveredPendingIntent, null);
      }
  
  }
  </code></pre>