How to get contact name for sms conversations?
Asked Answered
E

2

3

I'm retrieving list of last sms/mms conversations using following query:

String[] columns={"type", "address", "date", "body", "conversation_id"};
Cursor cursor=context.getContentResolver().query(Uri.parse("content://mms-sms/conversations"), columns,  null, null, "date desc");

Can anyone advise me how to get in the same query also contact name? Specifically field ContactsContract.PhoneLookup.DISPLAY_NAME?

I mean, I understand how to get those field in separate query, but I need to get it in the same query as for conversations.

Estas answered 17/9, 2012 at 6:10 Comment(2)
AFAIK,it's not possible because SMS manager just saves phone number of a person.you need to query it separately to contact api.Extraneous
If it would be ordinary SQL I could make smth with inner join and get display name in the same query. Why it's impossible in case of ContentResolver's - anyhow we understand that it's based on SQL queries...Estas
S
0

Try this:

Log.i(TAG,"load_contact for "+Phone_numb);
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(Phone_numb));
String name = "?";

ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, 
    new String[] { BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

try {
    if (contactLookup != null && contactLookup.getCount() > 0) {
        contactLookup.moveToNext();
        name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
    } else {
         return Phone_numb;
    }
} finally {
    if (contactLookup != null) {
        contactLookup.close();
    }
}
Shoelace answered 4/2, 2014 at 9:14 Comment(0)
R
0

I made this using this workaround:

val uri = Uri.parse("content://sms")
val projection = arrayOf("DISTINCT $THREAD_ID", _ID, ADDRESS, BODY, DATE, TYPE)
val selection = "$THREAD_ID IS NOT NULL) GROUP BY ($THREAD_ID"

contentResolver.query(uri, projection , selection, null, "date DESC")

If someone knows better approach, please, share it.

Ratoon answered 1/12, 2018 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.