3

I am getting all phone numbers from contact. Then i have checked all numbers using PhoneUtil.parse() method to get the number type. But it gets exception when the phone number is not saved with country code. For example the phone number is like 9876512343 or 09876512343 its an indian number. For this number I am getting parsing exception. If I am doing like this

PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "IN");

then its not a problem for that number. But how can i know that number is an indian number, so that i can pass IN in that argument. So if I can get the country code of a given phone number(String) then it can solve my problem.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Mamata Behera
  • 43
  • 1
  • 1
  • 6
  • possible duplicate http://stackoverflow.com/questions/5402253/getting-telephone-country-code-with-android – kalyan pvs Dec 05 '14 at 07:35
  • I think the link @kalyanpvs gave answers it. There is no way of finding out the country code by simply providing a contact. Use the array approach. – VipulKumar Dec 05 '14 at 08:02

2 Answers2

5

This is my working code:

TelephonyManager tm = (TelephonyManager) getApplicationContext()
                             .getSystemService(Context.TELEPHONY_SERVICE);
String phNo = tm.getLine1Number();
String country = tm.getSimCountryIso();

Log.d("PhoneNumber :", phNo);
Log.d("country :", country);

Add following line in AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
Md. Kamruzzaman
  • 1,895
  • 16
  • 26
  • 2
    OP does not want to find out country code for SIM number. she has various numbers extracted from Contacts (She mentioned it very clearly). – VipulKumar Dec 05 '14 at 08:01
  • thanks...The above solution helps to find out the country code of own device sim number. – Mamata Behera Dec 05 '14 at 09:54
  • 1
    Does this mean, that if I put a phone number into the address book, which doesn't have a prefix of the country, this one will be used to call ? Meaning that if I'm from Israel (prefix is "+972") , and I add a phone number "(0)50-1234567" , and I call this number, it actually calls "+972-50-1234567" ? – android developer Dec 13 '16 at 14:45
  • @androiddeveloper Pretty sure that's not how it works. Your phone will pass the digits without the prefix to your local telecommunications provider and they will route it locally. If the prefix is provided, it routes it as long distance, etc. – Joshua Pinter Jan 14 '17 at 20:52
  • @JoshPinter I see. Thanks. – android developer Jan 15 '17 at 09:56
  • "Does this mean, that if I put a phone number into the address book, which doesn't have a prefix of the country, this one will be used to call ? Meaning that if I'm from Israel (prefix is "+972") , and I add a phone number "(0)50-1234567" , and I call this number, it actually calls "+972-50-1234567" ? – android developer " -- same question i have so how identify in android? have you any solution ? @android developer – Sachin Suthar Oct 11 '17 at 07:28
2

There is nice library, in case you wouldn't like to READ_PHONE_STATE.

 implementation 'io.michaelrocks:libphonenumber-android:8.12.13'

Link to parent C++ library on github.

google/libphonenumber

and post for quick start here. Android. How to detect a country code by the phone number

Code example bellow:

        String NumberStr = user.getPhone();
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.createInstance(this.getContext());;
    try {
        Phonenumber.PhoneNumber NumberProto = phoneUtil.parse(NumberStr, null);
        if (!phoneUtil.isValidNumber(NumberProto)) {
            showError(getResources().getString(R.string.wrong_phone_number));
            return;
        }
        String regionISO = phoneUtil.getRegionCodeForCountryCode(NumberProto.getCountryCode());
        
        
    } catch (NumberParseException e) {
        showError(e.toString());
        return;
    }