How to get contact name for sms conversations?

这一生的挚爱 提交于 2019-12-01 06:08:23

问题


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.


回答1:


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.




回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/12453916/how-to-get-contact-name-for-sms-conversations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!