1

我正在尝试使用此代码获取传出彩信的收件人地址。

private String getAddressNumber(String id) {
    String selectionAdd = new String("msg_id=" + id);
    String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
    Uri uriAddress = Uri.parse(uriStr);
    Cursor cAdd = getContentResolver().query(uriAddress, null,
            selectionAdd, null, null);
    String name = null;
    if (cAdd.moveToFirst()) {
        do {
            String number = cAdd.getString(cAdd.getColumnIndex("address"));
            if (number != null) {
                try {
                    Long.parseLong(number.replace("-", ""));
                    name = number;
                } catch (NumberFormatException nfe) {
                    if (name == null) {
                        name = number;
                    }
                }
            }
        } while (cAdd.moveToNext());
    }
    if (cAdd != null) {
        cAdd.close();
    }
    return name;
}

但它返回“插入地址令牌”而不是实际地址。

4

2 回答 2

1

我遇到了同样的问题,经过一番探索,发现如下解决方案,mms很特别,因为它的外发消息不反映DATE字段中的发送时间,而且地址字段将是“插入地址令牌”, TYPE 为 151,要获取收件人号码,我们需要将几个表的查询组合在一起。:

private static String getAddressNumberOfRecipient(int threadId) {
    String selectionAdd = Telephony.Threads._ID + "=" + threadId;
    String uriStr = MessageFormat.format("content://mms-sms/conversations/{0}/recipients", threadId);
    Uri uriAddress = Uri.parse(uriStr);
    String[] columns = {Telephony.Threads.RECIPIENT_IDS};
    Cursor cAdd = context.getContentResolver().query(uriAddress, columns, selectionAdd, null, null);
    String name = null;
    if (cAdd.moveToFirst()) {
        do {
            name = cAdd.getString(cAdd.getColumnIndex(Telephony.Threads.RECIPIENT_IDS));
            if (!TextUtils.isEmpty(name)) {
                break;
            }
        }
        while (cAdd.moveToNext());
    }
    if (cAdd != null) {
        cAdd.close();
    }

    return TextUtils.isEmpty(name) ? "" : getCanonicalRecipient(Integer.parseInt(name.split(" ")[0]));
}

private static String getCanonicalRecipient(int recipientId) {
    String selectionAdd = Telephony.CanonicalAddressesColumns._ID + "=" + recipientId;
    String uriStr = MessageFormat.format("content://mms-sms/canonical-address/{0}", recipientId);
    Uri uriAddress = Uri.parse(uriStr);
    String[] columns = {Telephony.CanonicalAddressesColumns.ADDRESS};
    Cursor cAdd = context.getContentResolver().query(uriAddress, columns, selectionAdd, null, null);
    String name = null;
    if (cAdd.moveToFirst()) {
        do {
            name = cAdd.getString(cAdd.getColumnIndex(Telephony.CanonicalAddressesColumns.ADDRESS));
            if (!TextUtils.isEmpty(name)) {
                break;
            }
        }
        while (cAdd.moveToNext());
    }
    if (cAdd != null) {
        cAdd.close();
    }
    return TextUtils.isEmpty(name) ? "" : filterPhoneNumber(name);
}
于 2016-04-18T06:06:57.443 回答
0

尝试这个...

private String getAddressNumber(String id) {
        String selectionAdd = new String("msg_id=" + id);
        String uriStr = MessageFormat.format("content://mms/{0}/addr", id);
        Uri uriAddress = Uri.parse(uriStr);
        Cursor cursor = getContentResolver().query(uriAddress, null, selectionAdd, null, null);

        String phoneNum = null;
        if (cursor.moveToFirst()) {
            do {
                String number = cursor.getString(cursor.getColumnIndex("address"));
                if (number != null) {
                    boolean isNumberFormat = true;
                    try {
                        Long.parseLong(number.replace("-", ""));
                        phoneNum = number;
                    } catch (NumberFormatException e) { // ex) "insert-address-token"
//                        if (phoneNum == null) {
//                            phoneNum = number;
//                        }
                        isNumberFormat = false;
                    }
                    if (isNumberFormat)
                        break;
                }
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
            cursor.close();
        }
        return phoneNum;
    }
于 2017-11-12T23:20:23.053 回答