androidsmsinbox

Search sms Inbox


How can I search the sms inbox and show latest message from a special number. For example search for "999999999" and show last message received from this number. Is any way to do this?

I have use this code to return the count of messages my inbox

TextView view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  view = (TextView) findViewById(R.id.textView1);

  final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
  Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null);
  int unreadMessagesCount = c.getCount();
  c.deactivate();

  view.setText(String.valueOf(unreadMessagesCount));

Solution

  • If you only want the last message sent by a specific number use this:

    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
    Cursor cursor = getContentResolver().query(SMS_INBOX, null, "address = ?", 
            new String[] {"9999999999"}, "date desc limit 1");
    if(cursor.moveToFirst()) {
        // Do something 
        Log.v("Example", cursor.getString(cursor.getColumnIndex("body")));
    }
    cursor.close();
    

    The second answer to How many database columns associated with a SMS in android? is a great reference for different columns in an SMS.