Вопрос

I'm trying to get the recipient address of an outgoing MMS using this code.

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

But It's returning "insert-address-token" instead of the actual address.

Это было полезно?

Решение

I met the same problem, and after some exploration, figured it out as following solution, mms is special as its outgoing message does not reflect the sent time in DATE field, also, the address field will be "insert-address-token", and TYPE will be 151, to get recipient number, we need to combine several table's query together. :

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

Другие советы

try this...

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;
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top