5

I used this code

    String msgData = "";
    Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, null);
    cursor.moveToFirst();

    do{

       for(int idx=0;idx<cursor.getColumnCount();idx++)
       {
           msgData += " " + cursor.getColumnName(idx) + ":" + cursor.getString(idx);
       }
    }while(cursor.moveToNext());    

..and it works, but it returns more data than I want.

How to read 3 last sms (only msg and sender)?

Sam
  • 86,580
  • 20
  • 181
  • 179
user1824542
  • 225
  • 1
  • 4
  • 15
  • http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android – Omarj Nov 14 '12 at 18:07

1 Answers1

8

Simply sort the results by date and use the limit clause:

getContentResolver().query(SMS_INBOX, new String[] {body, address}, 
    null, null, "date desc limit 3");
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 1
    how to read only this: address:+48505073421 body:text message – user1824542 Nov 14 '12 at 18:22
  • Use `String string = "address: " + cursor.getString(1) + " body: " + cursor.getString(0);` – Sam Nov 14 '12 at 18:50
  • Please read through [Cursor's documentation](http://developer.android.com/reference/android/database/Cursor.html), you'll find `moveToPosition()` will help you do this. – Sam Nov 14 '12 at 19:11