使用“content:// mms-sms / conversations”获取多个联系人的对话

时间:2015-04-23 13:29:04

标签: android sms

我成功获取了列表中的所有会话,但是存在问题,如果会话中有多个“recipient_id”,则此代码表示无效的recipient_id 提前感谢您的帮助

public static void getAllMessagesInList(Context context) {

    Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
    String[] reqCols = new String[] { "_id", "recipient_ids", "message_count", "snippet", "date", "read" };
    Cursor cursor = context.getApplicationContext().getContentResolver().query(uri, reqCols, null, null, "date DESC");

    if (cursor != null && cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            MessageBoxObject messageBoxObject = new MessageBoxObject();

            messageBoxObject.setRecipient_ids(cursor.getString(cursor.getColumnIndex(reqCols[1])));
            messageBoxObject.setMessage_count(cursor.getString(cursor.getColumnIndex(reqCols[2])));
            messageBoxObject.setSnippet(cursor.getString(cursor.getColumnIndex(reqCols[3])));
            messageBoxObject.setDate(cursor.getLong(cursor.getColumnIndex(reqCols[4])));
            messageBoxObject.setRead(cursor.getInt(cursor.getColumnIndex(reqCols[5])));
            ConstantsValues.messageBoxObjects.add(messageBoxObject);
        }
    }
    cursor.close();

}

我的MessageBoxObject类是这样的 公共类MessageBoxObject {

String recipient_number;
String message_count;
String recipient_ids;
String snippet;
String readcount, snippet_cs, type, error, has_attachment, status;
Long date;
int read;

public int getRead() { return read; }

public void setRead(int read) { this.read = read; }

public Long getDate() { return date; }

public void setDate(Long date) { this.date = date; }

public String getRecipient_ids() { return recipient_ids; }

public void setRecipient_ids(String recipient_ids) { this.recipient_ids = recipient_ids; }

public String getMessage_count() { return message_count; }

public void setMessage_count(String message_count) { this.message_count = message_count; }

public String getRecipient_number() { return recipient_number; }

public void setRecipient_number(String recipient_number) { this.recipient_number = recipient_number; }

public String getSnippet() { return snippet; }

public void setSnippet(String snippet) { this.snippet = snippet; }

}

1 个答案:

答案 0 :(得分:2)

recipient_ids包含空格分隔的联系人ID。

因此,为了获取联系人列表,您必须为每个ID标记recipient_ids而不是查询content://mms-sms/canonical-address表。

cannonical-address表格_id与联系人address相关联。

您可以通过ContactsContract.PhoneLookup查询以获取联系信息。