CommonDataKinds.Phone.NORMALIZED_NUMBER反转字符串

时间:2015-01-21 14:22:18

标签: android normalization android-contacts phone-number

我想阅读Android中联系人的规范化数字,这是因为我使用了这段代码:

ContentResolver cr = currentActivity.getContentResolver();

    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    //Are contacts in the book?
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {  //Still contacts to save?
            //Does he have at least one telephone number?
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {


                String id   = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID) );
                String name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME) );
                ArrayList<String> numbers = new ArrayList<String>();
                ArrayList<String> normalizedNumbers = new ArrayList<String>();
                Log.e("Contactbook", "New Contacts: " + name);

                //Read numbers:
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {

                    if(!numbers.contains(pCur.getString(
                            pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
                    ))) {
                        numbers.add(pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
                        normalizedNumbers.add(pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER)));
                        allReadNumbers.add( numbers.get(numbers.size()  - 1) );
                        Log.e("Contactbook", "Number: " + numbers.get(numbers.size() - 1));
                        Log.e("Contactbook", "normalized: " + normalizedNumbers.get(normalizedNumbers.size() - 1));
                    }
                }
                contacts.add(new Contact(numbers, name ) );
                pCur.close();
            }
        }
    }

这适用于阅读数字,但奇怪的是它没有根据documentation给出标准化数字。相反,它只是反转数字字符串。

示例:

Number: 01602212855
normalized: 55821220610

什么可能导致这个错误?假设你每次都会得到一个标准化的数字,或者我应该自己规范化吗?如果是这样,哪个函数最适合它,基于PhoneNumberUtils 我想这将是formatNumberToE164(String phoneNumber, String defaultCountryIso),但我对defaultCountryIso有什么看法?

修改:我发现我无法使用formatNumberToE164(String phoneNumber, String defaultCountryIso),因为它需要的API级别高于我目前使用的级别。

0 个答案:

没有答案