title: Read inbox messages of a particular number and display them in an activity
body: <p>I am trying to read messages from an inbox and display them in an activity. My activity contains a button and listview. When I click on the button, all messages from the inbox are displayed. But my requirement is that I need to view messages from a particular number. My application is not running when I get messages from that number when I click on the click button only specific messages (associated with a particular number) must be updated.
  Here's the relevant code. </p>
  
  <p>myActivity.java:</p>
  
  <pre><code>public class SecureMessagesActivity extends Activity implements OnClickListener, OnItemClickListener {
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setTheme( android.R.style.Theme_Light );
          setContentView(R.layout.main);
          this.findViewById( R.id.UpdateList ).setOnClickListener( this );
      }
  
      ArrayList&lt;String&gt; smsList = new ArrayList&lt;String&gt;();
  
      public void onItemClick( AdapterView&lt;?&gt; parent, View view, int pos, long id ) {
          try {
              String[] splitted = smsList.get( pos ).split("\n"); 
              String sender = splitted[0];
              String encryptedData = "";
  
              for ( int i = 1; i &lt; splitted.length; ++i ) {
                  encryptedData += splitted[i];
              }
              String data = sender + "\n" + StringCryptor.decrypt( new String(SmsReceiver.PASSWORD), encryptedData );
              Toast.makeText( this, data, Toast.LENGTH_SHORT ).show();
          } 
          catch (Exception e) {
                  e.printStackTrace();
          }
      }
  
      public void onClick( View v ) {
          ContentResolver contentResolver = getContentResolver();
          Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inbox" ), null, null, null, null);
  
          int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );
          int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );
  
          if ( indexBody &lt; 0 || !cursor.moveToFirst() ) return;
  
          smsList.clear();
  
          do {
              String str = "Sender: " + cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );
              smsList.add( str );
          }
          while( cursor.moveToNext() );
  
          ListView smsListView = (ListView) findViewById( R.id.SMSList );
          smsListView.setAdapter( new ArrayAdapter&lt;String&gt;( this, android.R.layout.simple_list_item_1, smsList) );
          smsListView.setOnItemClickListener( this );
      }
  } 
  </code></pre>
  
  <p>SmsReceiver.java:</p>
  
  <pre><code>public class SmsReceiver extends BroadcastReceiver {
      public static final String SMS_EXTRA_NAME = "pdus";
      public static final String SMS_URI = "content://sms";
  
      public static final String ADDRESS = "address";
      public static final String PERSON = "person";
      public static final String DATE = "date";
      public static final String READ = "read";
      public static final String STATUS = "status";
      public static final String TYPE = "type";
      public static final String BODY = "body";
      public static final String SEEN = "seen";
  
      public static final int MESSAGE_TYPE_INBOX = 1;
      public static final int MESSAGE_TYPE_SENT = 2;
  
      public static final int MESSAGE_IS_NOT_READ = 0;
      public static final int MESSAGE_IS_READ = 1;
  
      public static final int MESSAGE_IS_NOT_SEEN = 0;
      public static final int MESSAGE_IS_SEEN = 1;
  
  
      public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 };
  
      public void onReceive( Context context, Intent intent ) {
  
          Bundle extras = intent.getExtras();
          String messages = "";
  
          if ( extras != null ) {
              Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
              ContentResolver contentResolver = context.getContentResolver();
  
              for ( int i = 0; i &lt; smsExtra.length; ++i ) {
                  SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
  
                  String body = sms.getMessageBody().toString();
                  String address = sms.getOriginatingAddress();
  
                  messages += "SMS from " + address + " :\n";                    
                  messages += body + "\n";
                  putSmsToDatabase( contentResolver, sms );
              }
  
              // Display SMS message
              Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
          }
  
          // WARNING!!! 
          // If you uncomment next line then received SMS will not be put to incoming.
          // Be careful!
          // this.abortBroadcast(); 
      }
  
      private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms ) {
          ContentValues values = new ContentValues();
          values.put( ADDRESS, sms.getOriginatingAddress() );
          values.put( DATE, sms.getTimestampMillis() );
          values.put( READ, MESSAGE_IS_NOT_READ );
          values.put( STATUS, sms.getStatus() );
          values.put( TYPE, MESSAGE_TYPE_INBOX );
          values.put( SEEN, MESSAGE_IS_NOT_SEEN );
          try {
              String encryptedPassword = StringCryptor.encrypt( new String(PASSWORD), sms.getMessageBody().toString() ); 
              values.put( BODY, encryptedPassword );
          }
          catch ( Exception e ) { 
              e.printStackTrace(); 
          }
  
          // Push row into the SMS table
          contentResolver.insert( Uri.parse( SMS_URI ), values );
      }
  }
  </code></pre>