-1

I am trying to get the country telephonic code by reading geolocation on app start.

But I would get only latitude and longitude. How to get the telephonic code from geolocation.

I have done this far see :

First in String xml I would create an array like this

<string-array name="CountryCodes" >
<item>91,IN</item>
.....
.....
.....
</string-array>

public String GetCountryZipCode(){
    String CountryID="";
    String CountryZipCode="";

    TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    //getNetworkCountryIso
    CountryID= manager.getSimCountryIso().toUpperCase();
    String[] rl=this.getResources().getStringArray(R.array.CountryCodes);
    for(int i=0;i<rl.length;i++){
        String[] g=rl[i].split(",");
        if(g[1].trim().equals(CountryID.trim())){
            CountryZipCode=g[0];
            break;  
        }
    }
    return CountryZipCode;
}

Now suppose I read geolocation but how would I know country name or country code(more easy if I get this)

SamH67
  • 319
  • 1
  • 4
  • 14

1 Answers1

0

According to documentation getSimCountryIso

Returns the ISO country code equivalent for the SIM provider's country code.

It has some (strange) behaviour have a look at Unexpected getSimCountryIso() behaviour for best experience you should use Mobile country code (MCC). You can find MCC in android using code below

TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (!TextUtils.isEmpty(networkOperator)) {
    int mcc = Integer.parseInt(networkOperator.substring(0, 3));
    int mnc = Integer.parseInt(networkOperator.substring(3));
}

You can find MCC list here and here. Save MCC list (from any of above links) in your application and find country code by MCC or MCCMNC.

Community
  • 1
  • 1
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
  • Harish but otp verification requires country phone code example - : 91 for India. I am having trouble to understand what MCC is? – SamH67 Jan 30 '17 at 05:50