2

I have a code which fetches phone number from mobile contacts using Content Resolver.I tested it on 2 mobiles.In one it fetches the country code and in another it doesnt.Is there a way to find out if a country code is present in a mobile number and if present I want to separate it from the number.Here is my code.Please help me

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();
    }
user8601021
  • 219
  • 3
  • 18
  • https://stackoverflow.com/questions/2543938/how-to-split-mobile-number-into-country-code-area-code-and-local-number – Shahab Rauf Nov 07 '17 at 07:21
  • I am already getting the phone numbers sometimes with the country code and sometimes not.I want to separate it – user8601021 Nov 07 '17 at 07:29

2 Answers2

2

You can use Android's PhoneNumberUtils.formatNumber.

If you're targeting only Lollipop and above devices:

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

If you're targeting older versions as well:

String formattedPhone;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    formattedPhone = PhoneNumberUtils.formatNumber(phone, Locale.getDefault().getCountry()));
} else {
    formattedPhone = PhoneNumberUtils.formatNumber(phone));
}
marmor
  • 27,641
  • 11
  • 107
  • 150
  • and what is phone?string of number fetched? – user8601021 Nov 07 '17 at 07:34
  • `formattedPhone` should format the number based on the current country formatting rules, without country-code. are you getting country codes for `formattedPhone`? – marmor Nov 07 '17 at 08:25
  • I am getting the complete phone no with and without country codes in my contact list – user8601021 Nov 07 '17 at 09:01
  • I think you might still be using your original phone your got in your query, and not the new `formattedPhone` value. Add `Log.d("PHONE", "formatted phone is: " + formattedPhone);` at the end of the code in my answer, and check the value of formattedPhone in the log – marmor Nov 07 '17 at 09:04
  • But I already displayed the formattedPhone output using Toast – user8601021 Nov 07 '17 at 09:07
-1

Getting telephone country code with Android This link worked for me.I am posting the link for all those looking for an answer like me

user8601021
  • 219
  • 3
  • 18