【问题标题】:How to get contact name for sms conversations?如何获取短信对话的联系人姓名?
【发布时间】:2012-09-09 08:29:39
【问题描述】:

我正在使用以下查询检索最近的短信/彩信对话列表:

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

谁能告诉我如何在同一个查询中获取联系人姓名?具体字段ContactsContract.PhoneLookup.DISPLAY_NAME?

我的意思是,我了解如何在单独的查询中获取这些字段,但我需要在与对话相同的查询中获取它。

【问题讨论】:

  • AFAIK,这是不可能的,因为短信管理器只是保存了一个人的电话号码。您需要单独查询才能联系api。
  • 如果它是普通的 SQL,我可以用 inner join 制作 smth 并在同一个查询中获取显示名称。为什么在 ContentResolver 的情况下它是不可能的 - 无论如何我们知道它是基于 SQL 查询......

标签: android sms cursor contacts


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    我使用这个解决方法做了这个:

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

    如果有人知道更好的方法,请分享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2012-01-11
      • 2016-05-07
      • 2012-10-11
      相关资源
      最近更新 更多