Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read last 3 sms from inbox in Android?

Tags:

android

sms

inbox

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)?

like image 767
user1824542 Avatar asked Nov 14 '12 18:11

user1824542


1 Answers

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");
like image 190
Sam Avatar answered Nov 15 '22 00:11

Sam