Search code examples
androidtextsmsmms

How to get only sent messages from specific conversation in Android Studio?


    ContentResolver cr = getContentResolver();
    String[] projection = new String[]{"body", "ct_t", "_id", "address"};
    Uri uri = Uri.parse("content://mms-sms/conversations/" + id);
    Cursor c = cr.query(uri, projection, null, null, null);

This queries all messages from a specific conversation and I would like only the sent messages to display as I need to distinguish each party of the conversation. Is there such a thing as a uri like this:

Uri uri2 = Uri.parse("content://mms-sms/conversations/" + id + "/sent/");

Solution

  • You'll need to add a couple of columns to your projection, and include a selection argument in the query() call.

    final String[] projection = {"_id", "address", "body", "ct_t", "type", "msg_box"};
    final String selection = "(type = 2 OR msg_box = 2)";
    final Uri uri = Uri.parse("content://mms-sms/conversations/" + threadId);
    Cursor c = getContentResolver().query(uri, projection, selection, null, null);
    

    An SMS message will have a type value of 2 when it has been sent. An MMS message will have the same value, but in the msg_box column.