1

I am trying to set phone code on appStartup.

Possible Solution: get country name from geocoder and after this I have xml raw resources where I have kept all country names,code,phoneCode.

Then created an activity to get all those in array list(getter and setter used)

Here is my Country List file:

public class ListCountry {
    static String TAG="Class Country";
    String nameCode;
    String phoneCode;


    public ListCountry(){}

    public ListCountry(String nameCode,String phoneCode){
        this.nameCode=nameCode;
        this.phoneCode=phoneCode;
    }

    public static List<ListCountry> readXMLofCountries(Context context){
        List<ListCountry> countries = new ArrayList<ListCountry>();
        try{
            XmlPullParserFactory xmlFactoryObject=XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser=xmlFactoryObject.newPullParser();
            InputStream ins=context.getResources().openRawResource(R.raw.countries);
            xmlPullParser.setInput(ins,null);
            int event=xmlPullParser.getEventType();
            while(event!=xmlPullParser.END_DOCUMENT){
                String name=xmlPullParser.getName();
                switch(event){
                    case XmlPullParser.START_TAG:
                        break;
                    case xmlPullParser.END_TAG:
                        if(name.equals("country")){
                            ListCountry country=new ListCountry();
                            country.setNameCode(xmlPullParser.getAttributeValue(null, "code").toUpperCase());
                            country.setPhoneCode(xmlPullParser.getAttributeValue(null, "phoneCode"));
                            country.setName(xmlPullParser.getAttributeValue(null, "name"));
                            countries.add(country);
                        }
                }
    event = xmlPullParser.next();

            }
        }
    return countries;
    }
    public String getNameCode() {
        return nameCode;
    }

    public void setNameCode(String nameCode) {
        this.nameCode = nameCode;
    }

    public String getPhoneCode() {
        return phoneCode;
    }

    public void setPhoneCode(String phoneCode) {
        this.phoneCode = phoneCode;
    }
}

Note: Ignore the catch block in this code since I didnt put that now.

Problem 1 : How do I get country code or country name on app start in onCreate method. Problem 2 : If I get country name or country code suppose India or IN where in XML

<country code='IN' phoneCode='91' name='India' />

Now how do get this phoneCode so that I could send otp using phoneCode+mobile_number.

Remember GOAL: User will not select country name or country code it will automatically set code which is my prime task or goal.

SamH67
  • 319
  • 1
  • 4
  • 14
  • Possible duplicate of [Getting telephone country code with Android](http://stackoverflow.com/questions/5402253/getting-telephone-country-code-with-android) – Prerak Sola Jan 30 '17 at 09:47
  • @Prerak Sola Using the telephony method would probably not be a good idea because the telephony method does not work with a device with no sim card – Karun Shrestha Jan 30 '17 at 09:54
  • @PrerakSola No this is not possible duplicate since getSimCountryIso has issues and suppose if there is dual sim then which number will it pick?? – SamH67 Jan 30 '17 at 09:55
  • @KarunShrestha Exactly the issue which I was reffering – SamH67 Jan 30 '17 at 09:55
  • @SamH67 what you can do is, get the location of the user, then get the country code. – Karun Shrestha Jan 30 '17 at 09:57
  • @KarunShrestha http://stackoverflow.com/questions/41920341/getting-telephonic-country-code-on-starting-app/41923962#41923962 you can also see the first solution from my previous question he told me some strange behavior about getSimCountryIso – SamH67 Jan 30 '17 at 09:59
  • @KarunShrestha that's what I asked how to get that location? and you know this will not be the best solution consider a scenario where user is travelling abroad and tries to install my app with his native phone number then it will pick place from other region while number is from other place. But I can't help this is what client wants. – SamH67 Jan 30 '17 at 10:00
  • @SamH67 wait, i'll write you a rough answer – Karun Shrestha Jan 30 '17 at 10:00
  • For the use case that user is using a device with no sim, you can use the API mentioned here: http://stackoverflow.com/a/26753534/4350275 – Prerak Sola Jan 30 '17 at 10:04
  • @PrerakSola the link which you have provided has another two links in solution part both are not openning and please see the comment below the answer also answer is not verified. Can you please remove the Duplicacy which you have branded to my question since i might not get answer from other user – SamH67 Jan 30 '17 at 10:09
  • I am able to open this: http://ip-api.com/docs/api:json – Prerak Sola Jan 30 '17 at 10:10
  • @PrerakSola Okay my company network was blocking it I saw that by using VPN – SamH67 Jan 30 '17 at 10:12
  • @PrerakSola read the comments for above link answer please – SamH67 Jan 30 '17 at 10:21

1 Answers1

1

First get the user's current location using Location Listener, or the new Fused Location. Then geocode the cordinates using the code below, here the line of code, countryName= address.getCountryName(); gives you the name of the country. Then in this same method, the line Log.d("codeee", new CountryCodes().getCode(countryName)); will access the class mentioned below called, CountryCodes, then return the country code of the respective country.:

 public void getAddressFromLocation(final double latitude, final double longitude) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            String countryName= null;
            try {
                List<Address> addressList = geocoder.getFromLocation(
                        latitude, longitude, 1);
                if (addressList != null && addressList.size() > 0) {
                    Address address = addressList.get(0);
                    countryName= address.getCountryName();
                }
            } catch (IOException e) {
                Log.e("Exception Error", "Unable to connect", e);
            } finally {
                Log.d("codeee", new CountryCodes().getCode(countryName));
            }
        }
    };
    thread.start();
}

Then, when you have the country name, use this class to get the country code:

public class CountryCodes {
    final Map<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

    public CountryCodes() {

        map.put("Andorra, Principality Of", "AD");
        map.put("United Arab Emirates", "AE");
        map.put("Afghanistan, Islamic State Of", "AF");
        map.put("Antigua And Barbuda", "AG");
        map.put("Anguilla", "AI");
        map.put("Albania", "AL");
        map.put("Armenia", "AM");
        map.put("Netherlands Antilles", "AN");
        map.put("Angola", "AO");
        map.put("Antarctica", "AQ");
        map.put("Argentina", "AR");
        map.put("American Samoa", "AS");
        map.put("Austria", "AT");
        map.put("Australia", "AU");
        map.put("Aruba", "AW");
        map.put("Azerbaidjan", "AZ");
        map.put("Bosnia-Herzegovina", "BA");
        map.put("Barbados", "BB");
        map.put("Bangladesh", "BD");
        map.put("Belgium", "BE");
        map.put("Burkina Faso", "BF");
        map.put("Bulgaria", "BG");
        map.put("Bahrain", "BH");
        map.put("Burundi", "BI");
        map.put("Benin", "BJ");
        map.put("Bermuda", "BM");
        map.put("Brunei Darussalam", "BN");
        map.put("Bolivia", "BO");
        map.put("Brazil", "BR");
        map.put("Bahamas", "BS");
        map.put("Bhutan", "BT");
        map.put("Bouvet Island", "BV");
        map.put("Botswana", "BW");
        map.put("Belarus", "BY");
        map.put("Belize", "BZ");
        map.put("Canada", "CA");
        map.put("Cocos (Keeling) Islands", "CC");
        map.put("Central African Republic", "CF");
        map.put("Congo, The Democratic Republic Of The", "CD");
        map.put("Congo", "CG");
        map.put("Switzerland", "CH");
        map.put("Ivory Coast (Cote D'Ivoire)", "CI");
        map.put("Cook Islands", "CK");
        map.put("Chile", "CL");
        map.put("Cameroon", "CM");
        map.put("China", "CN");
        map.put("Colombia", "CO");
        map.put("Costa Rica", "CR");
        map.put("Former Czechoslovakia", "CS");
        map.put("Cuba", "CU");
        map.put("Cape Verde", "CV");
        map.put("Christmas Island", "CX");
        map.put("Cyprus", "CY");
        map.put("Czech Republic", "CZ");
        map.put("Germany", "DE");
        map.put("Djibouti", "DJ");
        map.put("Denmark", "DK");
        map.put("Dominica", "DM");
        map.put("Dominican Republic", "DO");
        map.put("Algeria", "DZ");
        map.put("Ecuador", "EC");
        map.put("Estonia", "EE");
        map.put("Egypt", "EG");
        map.put("Western Sahara", "EH");
        map.put("Eritrea", "ER");
        map.put("Spain", "ES");
        map.put("Ethiopia", "ET");
        map.put("Finland", "FI");
        map.put("Fiji", "FJ");
        map.put("Falkland Islands", "FK");
        map.put("Micronesia", "FM");
        map.put("Faroe Islands", "FO");
        map.put("France", "FR");
        map.put("France (European Territory)", "FX");
        map.put("Gabon", "GA");
        map.put("Great Britain", "UK");
        map.put("Grenada", "GD");
        map.put("Georgia", "GE");
        map.put("French Guyana", "GF");
        map.put("Ghana", "GH");
        map.put("Gibraltar", "GI");
        map.put("Greenland", "GL");
        map.put("Gambia", "GM");
        map.put("Guinea", "GN");
        map.put("Guadeloupe (French)", "GP");
        map.put("Equatorial Guinea", "GQ");
        map.put("Greece", "GR");
        map.put("S. Georgia & S. Sandwich Isls.", "GS");
        map.put("Guatemala", "GT");
        map.put("Guam (USA)", "GU");
        map.put("Guinea Bissau", "GW");
        map.put("Guyana", "GY");
        map.put("Hong Kong", "HK");
        map.put("Heard And McDonald Islands", "HM");
        map.put("Honduras", "HN");
        map.put("Croatia", "HR");
        map.put("Haiti", "HT");
        map.put("Hungary", "HU");
        map.put("Indonesia", "ID");
        map.put("Ireland", "IE");
        map.put("Israel", "IL");
        map.put("India", "IN");
        map.put("British Indian Ocean Territory", "IO");
        map.put("Iraq", "IQ");
        map.put("Iran", "IR");
        map.put("Iceland", "IS");
        map.put("Italy", "IT");
        map.put("Jamaica", "JM");
        map.put("Jordan", "JO");
        map.put("Japan", "JP");
        map.put("Kenya", "KE");
        map.put("Kyrgyz Republic (Kyrgyzstan)", "KG");
        map.put("Cambodia, Kingdom Of", "KH");
        map.put("Kiribati", "KI");
        map.put("Comoros", "KM");
        map.put("Saint Kitts & Nevis Anguilla", "KN");
        map.put("North Korea", "KP");
        map.put("South Korea", "KR");
        map.put("Kuwait", "KW");
        map.put("Cayman Islands", "KY");
        map.put("Kazakhstan", "KZ");
        map.put("Laos", "LA");
        map.put("Lebanon", "LB");
        map.put("Saint Lucia", "LC");
        map.put("Liechtenstein", "LI");
        map.put("Sri Lanka", "LK");
        map.put("Liberia", "LR");
        map.put("Lesotho", "LS");
        map.put("Lithuania", "LT");
        map.put("Luxembourg", "LU");
        map.put("Latvia", "LV");
        map.put("Libya", "LY");
        map.put("Madagascar", "MG");
        map.put("Marshall Islands", "MH");
        map.put("Macedonia", "MK");
        map.put("Mali", "ML");
        map.put("Myanmar", "MM");
        map.put("Mongolia", "MN");
        map.put("Macau", "MO");
        map.put("Northern Mariana Islands", "MP");
        map.put("Martinique (French)", "MQ");
        map.put("Mauritania", "MR");
        map.put("Malta", "MT");
        map.put("Mauritius", "MU");
        map.put("Maldives", "MV");
        map.put("Malawi", "MW");
        map.put("Malaysia", "MY");
        map.put("Mexico", "MX");
        map.put("Moldavia", "MD");
        map.put("Monaco", "MC");
        map.put("Montserrat", "MS");
        map.put("Morocco", "MA");
        map.put("Mozambique", "MZ");
        map.put("Namibia", "NA");
        map.put("New Caledonia (French)", "NC");
        map.put("Niger", "NE");
        map.put("Norfolk Island", "NF");
        map.put("Nigeria", "NG");
        map.put("Nicaragua", "NI");
        map.put("Netherlands", "NL");
        map.put("Norway", "NO");
        map.put("Nepal", "NP");
        map.put("Nauru", "NR");
        map.put("Neutral Zone", "NT");
        map.put("Niue", "NU");
        map.put("New Zealand", "NZ");
        map.put("Oman", "OM");
        map.put("Panama", "PA");
        map.put("Peru", "PE");
        map.put("Polynesia (French)", "PF");
        map.put("Papua New Guinea", "PG");
        map.put("Philippines", "PH");
        map.put("Pakistan", "PK");
        map.put("Poland", "PL");
        map.put("Saint Pierre And Miquelon", "PM");
        map.put("Pitcairn Island", "PN");
        map.put("Puerto Rico", "PR");
        map.put("Portugal", "PT");
        map.put("Palau", "PW");
        map.put("Paraguay", "PY");
        map.put("Qatar", "QA");
        map.put("Reunion (French)", "RE");
        map.put("Romania", "RO");
        map.put("Russian Federation", "RU");
        map.put("Rwanda", "RW");
        map.put("Saudi Arabia", "SA");
        map.put("Solomon Islands", "SB");
        map.put("Seychelles", "SC");
        map.put("Sudan", "SD");
        map.put("Sweden", "SE");
        map.put("Singapore", "SG");
        map.put("Saint Helena", "SH");
        map.put("Slovenia", "SI");
        map.put("Svalbard And Jan Mayen Islands", "SJ");
        map.put("Slovak Republic", "SK");
        map.put("Sierra Leone", "SL");
        map.put("San Marino", "SM");
        map.put("Senegal", "SN");
        map.put("Somalia", "SO");
        map.put("Suriname", "SR");
        map.put("Saint Tome (Sao Tome) And Principe", "ST");
        map.put("Former USSR", "SU");
        map.put("El Salvador", "SV");
        map.put("Syria", "SY");
        map.put("Swaziland", "SZ");
        map.put("Turks And Caicos Islands", "TC");
        map.put("Chad", "TD");
        map.put("French Southern Territories", "TF");
        map.put("Togo", "TG");
        map.put("Thailand", "TH");
        map.put("Tadjikistan", "TJ");
        map.put("Tokelau", "TK");
        map.put("Turkmenistan", "TM");
        map.put("Tunisia", "TN");
        map.put("Tonga", "TO");
        map.put("East Timor", "TP");
        map.put("Turkey", "TR");
        map.put("Trinidad And Tobago", "TT");
        map.put("Tuvalu", "TV");
        map.put("Taiwan", "TW");
        map.put("Tanzania", "TZ");
        map.put("Ukraine", "UA");
        map.put("Uganda", "UG");
        map.put("United Kingdom", "UK");
        map.put("USA Minor Outlying Islands", "UM");
        map.put("United States", "US");
        map.put("Uruguay", "UY");
        map.put("Uzbekistan", "UZ");
        map.put("Holy See (Vatican City State)", "VA");
        map.put("Saint Vincent & Grenadines", "VC");
        map.put("Venezuela", "VE");
        map.put("Virgin Islands (British)", "VG");
        map.put("Virgin Islands (USA)", "VI");
        map.put("Vietnam", "VN");
        map.put("Vanuatu", "VU");
        map.put("Wallis And Futuna Islands", "WF");
        map.put("Samoa", "WS");
        map.put("Yemen", "YE");
        map.put("Mayotte", "YT");
        map.put("Yugoslavia", "YU");
        map.put("South Africa", "ZA");
        map.put("Zambia", "ZM");
        map.put("Zaire", "ZR");
        map.put("Zimbabwe", "ZW");

    }

    public String getCode(String country) {
        String countryFound = map.get(country);
        if (countryFound == null) {
            countryFound = "NP";
        }
        return countryFound;
    }
}

After this, if you need the country phone codes, create a class similar to this which will return phone codes with respect to the country code.

 public class CountryPhoneCodes {
    final Map<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);

    public CountryPhoneCodes() {

        map.put("NP", "977");
        map.put("US", "01");
        map.put("CN", "86");

    }

    public String getPhnCode(String countryCode) {
        String phnCountryFound = map.get(countryCode);
        if (phnCountryFound == null) {
            phnCountryFound = "977";
        }
        return phnCountryFound;
    }
}

This works, I've tested.

Chisko
  • 3,092
  • 6
  • 27
  • 45
Karun Shrestha
  • 731
  • 7
  • 16
  • Okay so how do I get location of a user in latitude and longitude? which I had to pass in this method to getCountryname then raw xml where all country codes are mentioned how do I make a check I am not getting anything – SamH67 Jan 30 '17 at 10:28
  • http://stackoverflow.com/questions/17519198/how-to-get-the-current-location-latitude-and-longitude-in-android maybe this so I got this country code then I have xml how to make a check – SamH67 Jan 30 '17 at 10:30
  • you don't need any xmls – Karun Shrestha Jan 30 '17 at 10:33
  • but then how will i get phoneCode that was my prime goal which still I dont see anything to get that – SamH67 Jan 30 '17 at 10:35
  • You can create another class, which will return the phone code respective to the country code, wait, i'll update the answer with a little of the example – Karun Shrestha Jan 30 '17 at 10:37
  • Sending Otp is my task but SMS gateway requires phone country code for international verification – SamH67 Jan 30 '17 at 10:37
  • but that's what I have done above please see info is in raw file and that List Country class has array list which I has all country code,phone code, country name – SamH67 Jan 30 '17 at 10:38
  • hmmm can you please provide me the list for all phone codes with corresponding country code list in both class mentioned above. This should be same – SamH67 Jan 30 '17 at 10:54
  • sorry, i don't have those, you can populate them yourself using google – Karun Shrestha Jan 30 '17 at 10:54
  • Karun bro listen I guess new change has come up since this answer is not efficient enough since it relies on GPS to location what if I dont turn on my GPS and my last location was UK but now it has changed to India and number is from India – SamH67 Jan 30 '17 at 10:58
  • New Update : Use Prerak Sola answer giving above in comment and when it wont have sim card then user will be prompted to write the code manually – SamH67 Jan 30 '17 at 11:00
  • That case is very rare, if something like that happens you can give user the option to select other countries – Karun Shrestha Jan 30 '17 at 11:00
  • can we know to read from sim slot 1 since if they have dual sim which slot will it pick and also see this http://stackoverflow.com/questions/7996197/detect-the-status-of-two-sim-cards-in-a-dual-sim-android-phone/7996639#7996639 – SamH67 Jan 30 '17 at 11:04
  • Since you have helped me a lot in that case I would upvote and verify your answer. Thank you! – SamH67 Jan 30 '17 at 11:05
  • Sorry, i have no idea about the dual sim thing. And thank you so much for considering doing that. Hope you get the answer that fits your requirements. – Karun Shrestha Jan 30 '17 at 11:06