Custom Cursor query to get data from content://sms/sent

688 Views Asked by At

I find this function in web android developer offficial

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal)

Now, I want to get sms from content://sms/sent , with thread_id of sms I will get only sms newest.

I will explain by example:

id   | thread_id |        phone     |       body

10       47
9        46 
8        47
7        45 
6        47 
5        43
4        45
3        42
2        41
1        47

For result I want to receive is:

 id   | thread_id |        phone     |       body

10       47
9        46     
7        45 
5        43
3        42
2        41

How must I do with query above? Thanks.

1

There are 1 best solutions below

0
On

You must use GROUP BY thread_id , see more details in my github repository SmsMessenger, you can get it by following query:

    Cursor cursor = MyApplication.getContext().getContentResolver().query(Uri.parse("content://sms")
            , null
            , "address IS NOT NULL) GROUP BY (thread_id"
            , null
            , null);
   if (cursor != null && cursor.moveToFirst()) { // must check the result to prevent exception
        do {
   String messageText = cursor.getString(cursor.getColumnIndexOrThrow("body"));
   System.out.println("messageText : "+messageText );
} while (cursor.moveToNext());