How to read last 3 sms from inbox in Android?
Asked Answered
O

1

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

Omari answered 14/11, 2012 at 18:0 Comment(1)
#849228Texture
J
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");
Jari answered 14/11, 2012 at 18:3 Comment(3)
how to read only this: address:+48505073421 body:text messageOmari
Use String string = "address: " + cursor.getString(1) + " body: " + cursor.getString(0);Jari
Please read through Cursor's documentation, you'll find moveToPosition() will help you do this.Jari

© 2022 - 2024 — McMap. All rights reserved.