将国家代码与手机号码分开(如果存在)android

separating country code from mobile number if present android

我有一个代码可以使用内容从移动联系人中获取 phone 号码 Resolver.I 在 2 个 mobiles.In 上测试过它,一个获取国家代码,另一个 doesnt.Is 有一种方法可以查明手机号码中是否存在国家代码,如果存在,我想将其与 number.Here 分开 code.Please 帮助我

ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if ((cur != null ? cur.getCount() : 0) > 0) {
        while (cur != null && cur.moveToNext()) {
            String id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));
            phoneContactName.add(name);

            if (cur.getInt(cur.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String countryCode = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                    phoneContactNos.add(phoneNo);
                    dup_phoneContactNos.add(phoneNo);
                    /*if (registeredContactNos.contains(phoneNo)) {
                        Toast.makeText(getApplicationContext(), "match found", Toast.LENGTH_SHORT).show();
                        selectedContactName.add(name);
                        selectedContactNos.add(phoneNo);
                        populateList(name, phoneNo);


                    }*/
                    Log.i(TAG, "Name: " + name);
                    Log.i(TAG, "Phone Number: " + phoneNo);
                }


                pCur.close();
            }
        }
        /*for(int i=0;i<selectedContactName.size();i++)
        {
            Toast.makeText(getApplicationContext(),selectedContactName.get(i)+","+selectedContactNos.get(i),Toast.LENGTH_SHORT).show();
        }*/
    }
    if (cur != null) {
        cur.close();
    }

您可以使用 Android 的 PhoneNumberUtils.formatNumber

如果您只针对 Lollipop 及更高版本的设备:

String formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));

如果您还针对旧版本:

String formattedPhone;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
} else {
    formattedPhone = PhoneNumberUtils.formatNumber(phone));
}

Getting telephone country code with Android 此 link 为 me.I 工作,正在为所有像我这样寻找答案的人发布 link