android收件人ID不会返回正确的号码

时间:2015-05-10 18:05:29

标签: android cursor sms uri contacts

我想通过使用sms-mms / conversations URI中的id来获取联系人号码

final Uri URI_SIMPLE = Uri.parse("content://mms-sms/conversations").buildUpon()
                    .appendQueryParameter("simple", "true").build();

我发现,当我运行查询并且它带回一个数字时,它不是我给出的id的正确数字。

public static String fetchNumberfromID(String contactID, Context c)
{
    Log.i("Utility", "*******ID********" + contactID);

    Cursor phones = c.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactID, null, null);

    //Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(phones));
    String number = null;

    while (phones.moveToNext())
    {
        number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

        switch (type)
        {
            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                // do something with the Home number here...
                break;
            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                // do something with the Mobile number here...
                break;
            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                // do something with the Work number here...
                break;
        }
    }
    phones.close();
    return number;
}

我做错了吗? recipient_id应该是正确使用的ID吗?或者是否有其他地方我应该尝试从他们没有存储在联系人中来获取数字?

1 个答案:

答案 0 :(得分:1)

URI "content://mms-sms/conversations"返回的recipient_id与相对未记录的规范地址表格中的_id字段相关联,该表格包含address字段下的电话号码

您可以使用以下个人收件人ID查询电话号码:

Cursor addressCursor = ctx.getContentResolver().query(Uri.parse("content://mms-sms/canonical-addresses"), null, "_id = " + recipientId, null, null);
String phoneNumber = addressCursor.getString(addressCursor.getColumnIndex("address"));