title: Display SMS message in TextView
body: <p>I'm writing a small app that when the phone receives SMS messages. It will show the sender phone number and the SMS body in a <code>TextView</code>. I have an SMS <code>BoardcastReceiver</code> and an <code>Activity</code>. </p>
  
  <p>Here is my SMS Listener. </p>
  
  <pre class="lang-java prettyprint-override"><code>import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.telephony.SmsManager;
  import android.telephony.SmsMessage;
  import android.util.Log;
  import android.widget.Toast;
  
  public class IncomingSms extends BroadcastReceiver {
  
      // Get the object of SmsManager
      final SmsManager sms = SmsManager.getDefault();
      BroadcastNewSms ourSMS;
  
      @Override
      public void onReceive(Context context, Intent intent) {
          // TODO Auto-generated method stub
          final Bundle bundle = intent.getExtras();
  
          try {
              if (bundle != null) {
                  final Object[] pdusObj = (Object[]) bundle.get("pdus");
  
                  for (int i = 0; i &lt; pdusObj.length; i++) {
                      SmsMessage currentMessage = SmsMessage
                              .createFromPdu((byte[]) pdusObj[i]);
                      String phoneNumber = currentMessage
                              .getDisplayOriginatingAddress();
  
                      String senderNum = phoneNumber;
                      String message = currentMessage.getDisplayMessageBody();
  
                      Log.i("SmsReciver", "senderNum: " + senderNum
                              + ", message: " + message);
                      //ourSMS.getSmsDetails(senderNum, message);
                      // Show SMS notification
                      int duration = Toast.LENGTH_LONG;
                      Toast toast = Toast.makeText(context, "senderNum: "
                              + senderNum + ", message: " + message, duration);
                      toast.show();
  
                  } // end of for loop
              } // bundle
  
          } catch (Exception e) {
              // TODO: handle exception
              Log.e("SmsReciver", "Exception smsReciver" + e);
          }
      }
  }
  </code></pre>
  
  <p>Here is my Activity:</p>
  
  <pre class="lang-java prettyprint-override"><code>import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class BroadcastNewSms extends Activity {
  
      TextView SMSm;
      String phoneNumber1;
      String SMSBody1;
  
      public void getSmsDetails(String phoneNumber, String SMSBody) {
          phoneNumber1 = phoneNumber;
          SMSBody1 = SMSBody;
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          SMSm = (TextView) findViewById(R.id.etSmsBody);
  
           SMSm.setText("Phone Number: " + phoneNumber1 + " " + "SMS: " +
           SMSBody1);
  
      }
  
  }
  </code></pre>
  
  <p>Here is my Manifest:</p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="kobi.avshalom.recivesms.BroadcastNewSms"
              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;receiver android:name="kobi.avshalom.recivesms.IncomingSms" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;  
      &lt;/application&gt;
  
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.READ_SMS" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.SEND_SMS" &gt;
      &lt;/uses-permission&gt;
  </code></pre>