Android Outgoing MMS获取地址

时间:2013-08-11 18:33:31

标签: android mms

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

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

但它返回“insert-address-token”而不是实际地址。

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,经过一番探索后,将其想象为以下解决方案,mms是特殊的,因为它的传出消息不反映DATE字段中的发送时间,而且,地址字段将是" insert -address-token"和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);
}

答案 1 :(得分: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;
    }