title: How can I read SMS messages programmatically in Android
body: <p>I want to read the message body of a new incoming SMS in android, programmatically. I tried something but that doesn't return any contents:</p>
  
  <p>Is there any error in my code ? </p>
  
  <pre><code>btnSendSms=(Button)findViewById(R.id.button1_SendSms);
              btnSendSms.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View arg0) {
                      // TODO Auto-generated method stub
  
                      String strCellNo1 , strCellNo2 , strCellNo3 , strCellNo4 , strCellNo5; 
                      SharedPreferences sendsmsSharedPref = Custom_Title_Bar_Activity.this.getSharedPreferences("MyPref",0);
  
                      strCellNo1 = sendsmsSharedPref.getString("num1", "n/a");
                      strCellNo2 = sendsmsSharedPref.getString("num2", "n/a");
                      strCellNo3 = sendsmsSharedPref.getString("num3", "n/a");
                      strCellNo4 = sendsmsSharedPref.getString("num4", "n/a");
                      strCellNo5 = sendsmsSharedPref.getString("num5", "n/a");
  
                      Log.e("Cell No One","-&gt;"+strCellNo1);
                      Log.e("Cell No Two","-&gt;"+strCellNo2);
                      Log.e("Cell No Three","-&gt;"+strCellNo3);
                      Log.e("Cell No Four","-&gt;"+strCellNo4);
                      Log.e("Cell No Five","-&gt;"+strCellNo5);
  
                      strPhones=strCellNo1;
  
                      if(strCellNo2.toString().length() &gt;1 )
                      {
                          strPhones=strPhones+";"+strCellNo2;
  
                          if(strCellNo3.toString().length() &gt;1 )
                          {
                              strPhones=strPhones+";"+strCellNo3;
  
                              if(strCellNo4.toString().length() &gt;1 )
                              {
                                  strPhones=strPhones+";"+strCellNo4;
  
                                  if(strCellNo5.toString().length() &gt;1 )
                                  {
                                      strPhones=strPhones+";"+strCellNo5;
  
                                  }
  
                              }
  
                          }
  
                      }
  
  
  
                      String message="Android";
                      txtSms=(TextView)findViewById(R.id.txt_Sms);
                      txtSms.setText(message);
                      String strMessage=txtSms.getText().toString();
  
                      /*strPhones=strCellNo1+";"+strCellNo2+";"+strCellNo3+";"+strCellNo4+";"+strCellNo5;*/
                      Log.e("String Phone-&gt;",""+strPhones);
                      edtxt_PhNO=(TextView)findViewById(R.id.txt_PhNO);
                      edtxt_PhNO.setText(strPhones);
                      String strPHNO=edtxt_PhNO.getText().toString();
  
                      StringTokenizer stokenizer=new StringTokenizer(strPHNO,";");
                      Log.e("",""+stokenizer);
  
  
                      while(stokenizer.hasMoreElements())
                      {
                          Log.e("Inside While Loop ","");
                          String tempMobileNumber=(String)stokenizer.nextElement();
                          Log.e("",""+tempMobileNumber);
                          if(tempMobileNumber.length() &gt;0 &amp;&amp; strMessage.trim().length() &gt;0 )
                          {
                              sendSMS(tempMobileNumber, strMessage);
                              Log.e("sendSMS Method","");
                          }
                          else
                          {
                              Toast.makeText(getBaseContext(),"Please enter both phone number and message.",Toast.LENGTH_SHORT).show();
                          }
                      }
                  }
              });
  
  
  
      private void sendSMS(String phoneNumber, String message)
          {
              String SENT = "SMS_SENT";
              String DELIVERED = "SMS_DELIVERED";
  
              PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                  new Intent(SENT), 0);
  
              PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                  new Intent(DELIVERED), 0);
  
            //---when the SMS has been sent---
              registerReceiver(new BroadcastReceiver(){
                  @Override
                  public void onReceive(Context arg0, Intent arg1) {
                      switch (getResultCode())
                      {
                          case Activity.RESULT_OK:
                              Toast.makeText(getBaseContext(), "SMS sent", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                          case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                              Toast.makeText(getBaseContext(), "Generic failure", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                          case SmsManager.RESULT_ERROR_NO_SERVICE:
                              Toast.makeText(getBaseContext(), "No service", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                          case SmsManager.RESULT_ERROR_NULL_PDU:
                              Toast.makeText(getBaseContext(), "Null PDU", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                          case SmsManager.RESULT_ERROR_RADIO_OFF:
                              Toast.makeText(getBaseContext(), "Radio off", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                      }
                  }
              },new IntentFilter(SENT));
  
              //---when the SMS has been delivered---
              registerReceiver(new BroadcastReceiver(){
                  @Override
                  public void onReceive(Context arg0, Intent arg1) {
                      switch (getResultCode())
                      {
                          case Activity.RESULT_OK:
                              Toast.makeText(getBaseContext(), "SMS delivered", 
                                      Toast.LENGTH_SHORT).show();
                              break;
                          case Activity.RESULT_CANCELED:
                              Toast.makeText(getBaseContext(), "SMS not delivered", 
                                      Toast.LENGTH_SHORT).show();
                              break;                        
                      }
                  }
              }, new IntentFilter(DELIVERED));        
  
             /* SmsManager sms = SmsManager.getDefault();
              sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       */
  
  
              SmsManager sms = SmsManager.getDefault();
              sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);  
          }
  
      and this is SmsReceiver class extends Broadcast Receiver
  
  
      public class SmsReceiver extends BroadcastReceiver
      {
  
          @Override
          public void onReceive(Context context, Intent intent) {
              // TODO Auto-generated method stub
  
               //---get the SMS message passed in---
  
              Bundle bundle = intent.getExtras();
              if (bundle != null) 
              {
                  Object[] pdus = (Object[]) bundle.get("pdus");
                  final SmsMessage[] messages = new SmsMessage[pdus.length];
  
  
                  for (int i = 0; i &lt; pdus.length; i++) 
                  {
                      messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                  }
  
                  StringBuffer content = new StringBuffer();
                  if (messages.length &gt; 0) 
                  {
                      for (int i = 0; i &lt; messages.length; i++) 
                      {
                          content.append(messages[i].getMessageBody());
                      }
                  }
  
  
                  String mySmsText = content.toString();
              }
          }
      }
  </code></pre>
  
  <p>Can anyone help me with this?</p>